using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class Thread_Test : MonoBehaviour
{
/// <summary>
/// 信号灯
/// </summary>
private ManualResetEvent signal;
private List<Thread> threadLst = new List<Thread>();
private void Start()
{
signal = new ManualResetEvent(false);
Fun1();
}
private void Fun1()
{
Thread thread01 = new Thread(Fun2);
thread01.Start();
threadLst.Add(thread01);
Thread thread02 = new Thread(Fun3);
thread02.Start(5);
threadLst.Add(thread02);
}
private void Fun2()
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
Debug.Log($"Fun2被执行:{i}");
signal.WaitOne(); //线程等待
}
}
private void Fun3(object obj)
{
int len = (int)(obj);
for (int i = 0; i < len; i++)
{
Thread.Sleep(1000);
Debug.Log($"Fun3被执行:{i}");
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
//暂停
signal.Reset();
}
if (Input.GetKeyDown(KeyCode.S))
{
//恢复
signal.Set();
}
}
private void OnApplicationQuit()
{
if (threadLst.Count>0)
{
for (int i = 0; i < threadLst.Count; i++)
{
threadLst[i].Abort();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class Thread_时间 : MonoBehaviour
{
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
private string timer;
private int second = 120;
private void Start()
{
ThreadPool.QueueUserWorkItem((obj) =>
{
UpdateTimer(cancellationTokenSource.Token);
});
ThreadPool.QueueUserWorkItem(CallBack, cancellationTokenSource.Token);
}
private void UpdateTimer(CancellationToken token)
{
while (second >= 0)
{
if (token.IsCancellationRequested)
{
Debug.Log("线程池操作被释放");
break;
}
Thread.Sleep(1000);
second--;
timer = $"{second / 60}:{second % 60}";
Debug.Log(timer);
}
}
private void CallBack(object state)
{
Thread.Sleep(1000);
PrintMessage("异步方法开始");
CancellationToken token = (CancellationToken)state;
UpdateTimer(token);
}
private void PrintMessage(string data)
{
int workThreadNum;
int IOThreadNum;
ThreadPool.GetAvailableThreads(out workThreadNum, out IOThreadNum);
Debug.Log($"{data}\n CurrentThreadID is {Thread.CurrentThread.ManagedThreadId}\n" +
$"CurrentThread is background:{Thread.CurrentThread.IsBackground.ToString()}\n" +
$"workThreadNum is {workThreadNum}\n" +
$"IOThreadNum is {IOThreadNum}");
}
private void OnApplicationQuit()
{
cancellationTokenSource.Cancel();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class Thread_线程池 : MonoBehaviour
{
private static object lockObj = new object();
ManualResetEvent manualResetEvent;
private void Start()
{
ThreadPool.QueueUserWorkItem(Fun1);
}
private void Fun1(object state)
{
lock (lockObj)
{
Fun2();
}
manualResetEvent?.Set();
}
private void Fun2()
{
Debug.Log($"通过对象池被调用,当前的线程ID:{Thread.CurrentThread.ManagedThreadId}");
}
}
using Common;
using System;
using System.Collections.Generic;
/// <summary>
/// 线程交叉访问助手类
/// 跨线程访问unityAPI
/// </summary>
public class ThreadCrossHelper : MonoSingleton<ThreadCrossHelper>
{
class DelayedItem
{
public Action CurrentAction { get; set; }
public DateTime Time { get; set; }
}
private List<DelayedItem> delayedItemLst;
public override void Init()
{
base.Init();
delayedItemLst = new List<DelayedItem>();
}
private void Update()
{
lock (delayedItemLst)
{
for (int i = delayedItemLst.Count - 1; i >= 0; i--)
{
if (delayedItemLst[i].Time <= DateTime.Now)
{
if (delayedItemLst[i].CurrentAction != null)
{
delayedItemLst[i].CurrentAction();
delayedItemLst.RemoveAt(i);
}
}
}
}
}
/// <summary>
/// 在主线程执行
/// </summary>
public void ExecuteOnMainThread(Action cb, float delay = 0f)
{
lock (delayedItemLst)
{
var item = new DelayedItem()
{
CurrentAction = cb,
Time = DateTime.Now.AddSeconds(delay)
};
delayedItemLst.Add(item);
}
}
}