Unity3d多线程

http://blog.csdn.net/dingkun520wy/article/details/49181645

(一)多线程的创建

Thread t = new Thread(new ThreadStart(Go)); 

Thread t1 = new Thread(Go);

两种创建方式没有区别;

(二)多线程的状态控制和优先级

多线程有4种状态:Start()开始;Abort()终止;Join()阻塞;Sleep()休眠;

有5种优先级:从高到底依次为:Highest,AboveNormal ,Normal ,BelowNormal ,Lowest;

线程的默认优先级为Normal;


多线程实例

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  *  
  3.  * 游戏多线程 
  4.  * */  
  5. using UnityEngine;  
  6. using System.Threading;  
  7.   
  8.   
  9. public class BaseThread{  
  10.       
  11.     private static BaseThread instance;  
  12.   
  13.     object obj = new object();  
  14.     int num = 0;  
  15.     private BaseThread()  
  16.     {  
  17.   
  18.         /*测试线程优先级  
  19.         /*/  
  20.         Thread th1 = new Thread(Th_test1);              //创建一个线程  
  21.         Thread th2 = new Thread(Th_test2);  
  22.         Thread th3 = new Thread(Th_test3);  
  23.         th1.Start();  
  24.         th2.Start();  
  25.         th3.Start();  
  26.         //学习优先级  
  27.         th1.Priority = System.Threading.ThreadPriority.Highest;         //优先级最高  
  28.         th2.Priority = System.Threading.ThreadPriority.Normal;  
  29.         th3.Priority = System.Threading.ThreadPriority.Lowest;  
  30.         //**/  
  31.   
  32.   
  33.         ///*测试线程锁  
  34.           
  35.         /*/  
  36.   
  37.         Thread th1 = new Thread(new ThreadStart(Th_lockTest));  
  38.         th1.Name = "test1";  
  39.         th1.Start();  
  40.   
  41.         Thread th2 = new Thread(new ThreadStart(Th_lockTest));  
  42.         th2.Name = "test2";  
  43.         th2.Start();  
  44.   
  45.         //*/  
  46.   
  47.   
  48.   
  49.   
  50.     }  
  51.   
  52.   
  53.     public static BaseThread GetInstance()    
  54.     {  
  55.         if (instance == null)    
  56.         {  
  57.             instance = new BaseThread();    
  58.         }    
  59.         return instance;   
  60.     }  
  61.       
  62.   
  63.     //测试多线程锁  
  64.     public void Th_lockTest()  
  65.     {  
  66.           
  67.         Debug.Log("测试多线程");  
  68.         while (true)  
  69.         {  
  70.             lock (obj)  
  71.             {                                //线程“锁”           
  72.                 num++;  
  73.                 Debug.Log(Thread.CurrentThread.Name + "测试多线程" + num);  
  74.             }  
  75.             Thread.Sleep(100);  
  76.             if (num > 300)  
  77.             {  
  78.                 Thread.CurrentThread.Abort();  
  79.             }  
  80.         }  
  81.     }  
  82.   
  83.     //测试多线程优先级  
  84.     public void Th_test1()  
  85.     {  
  86.         for (int i = 0; i < 500; i++)  
  87.         {  
  88.              
  89.             Debug.Log("测试多线程1执行的次数:" + i);  
  90.             if(i >200)  
  91.             {  
  92.                 Thread.CurrentThread.Abort();  
  93.             }  
  94.         }  
  95.     }  
  96.     public void Th_test2()  
  97.     {  
  98.         for (int i = 0; i < 500; i++)  
  99.         {  
  100.            
  101.             Debug.Log("测试多线程2执行的次数:" + i);  
  102.             if (i > 300)  
  103.             {  
  104.                 Thread.CurrentThread.Abort();  
  105.             }  
  106.         }  
  107.     }  
  108.     public void Th_test3()  
  109.     {  
  110.         for (int i = 0; i < 500; i++)  
  111.         {  
  112.         
  113.             Debug.Log("测试多线程3执行的次数:" + i);  
  114.             if (i > 400)  
  115.             {  
  116.                 Thread.CurrentThread.Abort();  
  117.             }  
  118.         }  
  119.     }  
  120.   
  121. }  

注意:

1,当多个线程同时访问同一数据时要加线程锁lock。

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. Object n=new Object();  
    2.    long shu = 0;    
    3. // Use this for initialization  
    4. void Start () {  
    5.          
    6. }  
    7.   
    8. // Update is called once per frame  
    9. void Update ()   
    10.    {  
    11.        lock (n)  
    12.        {  
    13.            xian = 1;  
    14.        }  
    15. }  
原文地址:https://www.cnblogs.com/alps/p/5625598.html