using System; using System.Collections; using System.Collections.Generic; using System.Threading; using UnityEngine; using Random = System.Random; public class MyThreadManger : MonoBehaviour { Hashtable hashtable = new Hashtable(); private int m_iParam=0;//随便一个类型的参数 private static string name = "1";//随便一个类型的参数 void Start() { ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod), name);//将方法添加进线程池,并传入参数 ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod2), name);//将方法添加进线程池,并传入参数 } private void MyMethod(object param) { Random ran = new Random(100); for (int i = 0; i < 100; i++) { try { hashtable[i] = param; param = Convert.ToString(ran.Next()); Debug.Log("开了一个线程:" + hashtable[i + "11~"].ToString()); Thread.Sleep(100); } catch (Exception ex) { Debug.LogWarning(ex); } } } private void MyMethod2(object param) { Random ran = new Random(200); for (int i = 0; i < 100; i++) { try { hashtable[i + "11~"] = param; param = Convert.ToString(ran.Next(2)); Debug.Log("开了第二个线程:" + hashtable[i + "11~"].ToString()); Thread.Sleep(100); } catch (Exception ex) { Debug.LogWarning(ex); } } } }