上篇文件介绍了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();
}
}
}