Unity-线程工具(二)

40 篇文章 3 订阅

上篇文件介绍了MainThreadHelper类。本篇继续介绍SingleThreadHelper.cs类。它封装了开启单线程Thread处理单任务的接口。并对系统中所有建的线程做了统一的控制管理。毕竟线程也是非常耗性能的。可以用来处理带参数或不带参数的任务。

实现如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
using System;
/*
* Author:W
* 单任务独立非主线程处理工具
*/
namespace W.GameFramework.MultiThread
{
	/// <summary>
	/// 非主线程的开始入口封装
	/// </summary>
	public class SingleThreadStarter
	{
		private ThreadStart threadStart;
		private ParameterizedThreadStart parameterizedThreadStart;

		public SingleThreadStarter(ThreadStart threadStart)
		{
			this.threadStart = threadStart;
		}

		public SingleThreadStarter(ParameterizedThreadStart parameterizedThreadStart)
		{
			this.parameterizedThreadStart = parameterizedThreadStart;
		}

		/// <summary>
		/// 非主线程的入口构造
		/// 【不带参数】
		/// </summary>
		public void ExcuteThreadStart()
		{
			try
			{
				threadStart();
			}
			catch (Exception e)
			{
				//NotDo
			}
		}

		/// <summary>
		/// 非主线程的入口构造
		/// 【带参数】
		/// </summary>
		/// <param name="param"></param>
		public void ExcuteParamThreadStart(object param)
		{
			try
			{
				parameterizedThreadStart(param);
			}
			catch (Exception e)
			{ 
			   //NotDo
			}
		}


	}

	/// <summary>
	/// 单任务:非主线程处理工具
	/// 1.开启子线程处理不带参数的任务
	/// 2.开启子线程处理带参数的任务
	/// </summary>
	public class SingleThreadHelper
	{		
		/// <summary>
		/// 已经开启的子线程集
		/// </summary>
		private static List<Thread> threads = new List<Thread>();

		/// <summary>
		/// 初始化
		/// </summary>
		public static void Init()
		{
			if (threads == null)
				threads = new List<Thread>();
		}

		/// <summary>
		/// 非主线程执行任务
		/// 【不带参数】
		/// </summary>
		/// <param name="threadStart"></param>
		/// <param name="priority"></param>
		/// <returns></returns>
		public static Thread DealTaskBySingleThread(ThreadStart threadStart, System.Threading.ThreadPriority priority = System.Threading.ThreadPriority.Normal)
		{
			ValidateThreadFilter();

			Thread thread = null;

			SingleThreadStarter singleThreadStarter = new SingleThreadStarter(threadStart);
			thread = new Thread(singleThreadStarter.ExcuteThreadStart);
			thread.Priority = priority;
			threads.Add(thread);
			thread.Start();

			return thread;
		}

		/// <summary>
		/// 非主线程执行任务
		/// 【带参数】
		/// </summary>
		/// <param name="parameterizedThreadStart"></param>
		/// <param name="param"></param>
		/// <param name="priority"></param>
		/// <returns></returns>
		public static Thread DealTaskBySingleThread(ParameterizedThreadStart parameterizedThreadStart,object param, System.Threading.ThreadPriority priority = System.Threading.ThreadPriority.Normal)
		{
			ValidateThreadFilter();

			Thread thread = null;

			SingleThreadStarter singleThreadStarter = new SingleThreadStarter(parameterizedThreadStart);
			thread = new Thread(singleThreadStarter.ExcuteParamThreadStart);
			thread.Priority = priority;
			threads.Add(thread);
			thread.Start(param);

			return thread;
		}


		/// <summary>
		/// 检查过滤掉没有在运行的子线程
		/// </summary>
		private static void ValidateThreadFilter()
		{
			for (int i = 0; i < threads.Count; i++)
			{
				Thread curThread = threads[i];
				if (curThread.ThreadState == ThreadState.Aborted ||
				   curThread.ThreadState == ThreadState.AbortRequested ||
				   curThread.ThreadState == ThreadState.Stopped ||
				   curThread.ThreadState == ThreadState.StopRequested ||
				   curThread.ThreadState == ThreadState.Unstarted)
				{
					threads.RemoveAt(i);
				}
			}
		}


		/// <summary>
		/// 停止所有在运行的线程
		/// </summary>
		public static void AbortRunningThreads()
		{
			ValidateThreadFilter();

			for (int i = 0; i < threads.Count; i++)
			{
				//只可以中断处于WaitSleepJoin状态的线程,当线程不为WaitSleepJoin时,线程将恢复执行
				threads[i].Interrupt();
				threads[i].Join(100);
			}

			threads.Clear();
		}	
	}
}

下篇文章链接点击进入 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Data菌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值