From bd518e96694a3f65bd16abc76ebf7bd76f1ddf10 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Fri, 27 May 2022 21:15:26 +0800 Subject: [PATCH] MultiThreadMgr MultiThreadMgr --- .../TEngine/Runtime/Thread/MultiThreadMgr.cs | 151 ++++++++++++++++++ .../Runtime/Thread/MultiThreadMgr.cs.meta | 11 ++ 2 files changed, 162 insertions(+) create mode 100644 Assets/TEngine/Runtime/Thread/MultiThreadMgr.cs create mode 100644 Assets/TEngine/Runtime/Thread/MultiThreadMgr.cs.meta diff --git a/Assets/TEngine/Runtime/Thread/MultiThreadMgr.cs b/Assets/TEngine/Runtime/Thread/MultiThreadMgr.cs new file mode 100644 index 00000000..18133cd8 --- /dev/null +++ b/Assets/TEngine/Runtime/Thread/MultiThreadMgr.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Threading; +using UnityEngine; + +namespace TEngine +{ + /// + /// 异步任务,交给子线程执行 + /// + public abstract class AsyncTask + { + protected MultiThreadMgr MultiThreadMgr + { + get + { + return MultiThreadMgr.Instance; + } + } + + /// + /// 执行任务 + /// + public void Execute() + { + try + { + Run(); + } + finally + { + if (!Loop) + { + MultiThreadMgr.FinishTask(this); + } + } + } + + /// + /// 关闭执行 + /// + public abstract void Close(); + + /// + /// 开始运行 + /// + public abstract void Run(); + + public bool Loop = false; + } + + /// + /// 多线程管理器 + /// + public class MultiThreadMgr : UnitySingleton + { + // 需要在主线程执行的操作 + static List actions = new List(); + static List runningActions = new List(); + static object obj = new object(); + + // 异步任务队列 + static List taskList = new List(); + static Dictionary threads = new Dictionary(); + + /// + /// 在主线程中执行 + /// + /// Action. + public void RunOnMainThread(Action action) + { + lock (obj) + { + actions.Add(action); + } + } + + /// + /// 添加异步任务 + /// + /// Runnable. + public void AddAsyncTask(AsyncTask runnable) + { + TLogger.LogInfo($"AddTask:{ runnable}"); + taskList.Add(runnable); + Thread thread = new Thread(runnable.Execute); + threads.Add(runnable,thread); + thread.IsBackground = true; + thread.Start(); + } + + /// + /// 完成异步任务 + /// + /// Runnable. + public void FinishTask(AsyncTask runnable) + { + runnable.Close(); + taskList.Remove(runnable); + threads[runnable].Abort(); + threads.Remove(runnable); + TLogger.LogInfo($"RemoveTask:{runnable},{taskList.Count}"); + } + + /// + /// 主线程更新 + /// + void Update() + { + lock (obj) + { + runningActions.Clear(); + runningActions.AddRange(actions); + actions.Clear(); + } + + // 处理主线程事件 + if (runningActions.Count > 0) + { + foreach (Action action in runningActions) + { + action(); + } + } + runningActions.Clear(); + } + + protected override void OnDestroy() + { + for (int i = 0; i < taskList.Count; i++) + { + taskList[i].Close(); + } + + var etr = threads.GetEnumerator(); + + while (etr.MoveNext()) + { + var thread = etr.Current.Value; + thread.Abort(); + } + + etr.Dispose(); + + threads.Clear(); + + base.OnDestroy(); + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/Thread/MultiThreadMgr.cs.meta b/Assets/TEngine/Runtime/Thread/MultiThreadMgr.cs.meta new file mode 100644 index 00000000..6ed8c682 --- /dev/null +++ b/Assets/TEngine/Runtime/Thread/MultiThreadMgr.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: df75d01709b5c0249bbf3705717b9b37 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: