Unity3d多线程

(一)多线程的创建

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

Thread t1 = new Thread(Go);

两种创建方式没有区别;

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

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

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

线程的默认优先级为Normal;


多线程实例

/*
 * 
 * 游戏多线程
 * */
using UnityEngine;
using System.Threading;


public class BaseThread{
	
	private static BaseThread instance;

    object obj = new object();
    int num = 0;
    private BaseThread()
	{

        /*测试线程优先级 
        /*/
        Thread th1 = new Thread(Th_test1);              //创建一个线程
        Thread th2 = new Thread(Th_test2);
        Thread th3 = new Thread(Th_test3);
        th1.Start();
        th2.Start();
        th3.Start();
        //学习优先级
        th1.Priority = System.Threading.ThreadPriority.Highest;         //优先级最高
        th2.Priority = System.Threading.ThreadPriority.Normal;
        th3.Priority = System.Threading.ThreadPriority.Lowest;
        //**/


        ///*测试线程锁
        
        /*/

        Thread th1 = new Thread(new ThreadStart(Th_lockTest));
        th1.Name = "test1";
        th1.Start();

        Thread th2 = new Thread(new ThreadStart(Th_lockTest));
        th2.Name = "test2";
        th2.Start();

		//*/




    }


    public static BaseThread GetInstance()  
	{
		if (instance == null)  
		{
            instance = new BaseThread();  
		}  
		return instance; 
	}
	

    //测试多线程锁
    public void Th_lockTest()
    {
        
        Debug.Log("测试多线程");
        while (true)
        {
            lock (obj)
            {                                //线程“锁”         
                num++;
                Debug.Log(Thread.CurrentThread.Name + "测试多线程" + num);
            }
            Thread.Sleep(100);
            if (num > 300)
            {
                Thread.CurrentThread.Abort();
            }
        }
    }

    //测试多线程优先级
    public void Th_test1()
    {
        for (int i = 0; i < 500; i++)
        {
           
            Debug.Log("测试多线程1执行的次数:" + i);
            if(i >200)
            {
                Thread.CurrentThread.Abort();
            }
        }
    }
    public void Th_test2()
    {
        for (int i = 0; i < 500; i++)
        {
         
            Debug.Log("测试多线程2执行的次数:" + i);
            if (i > 300)
            {
                Thread.CurrentThread.Abort();
            }
        }
    }
    public void Th_test3()
    {
        for (int i = 0; i < 500; i++)
        {
      
            Debug.Log("测试多线程3执行的次数:" + i);
            if (i > 400)
            {
                Thread.CurrentThread.Abort();
            }
        }
    }

}



 

转载于:https://www.cnblogs.com/lexiaoyao-jun/p/5208255.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值