博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3d多线程
阅读量:6854 次
发布时间:2019-06-26

本文共 2533 字,大约阅读时间需要 8 分钟。

(一)多线程的创建

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

你可能感兴趣的文章
k8s拾遗 - Secret
查看>>
Android SparseArray 原理解析
查看>>
PHP类的定义
查看>>
Composer 中国镜像地址配置
查看>>
比特币暴跌后的连锁反应
查看>>
Python爬虫入门教程 62-100 30岁了,想找点文献提高自己,还被反爬了,Python搞起,反爬第2篇...
查看>>
第80节:Java中的MVC设计模式
查看>>
区块链100讲:以实例形式深入浅出讲透BANCOR算法
查看>>
Java并发编程 深入剖析volatile关键字
查看>>
Vue基础
查看>>
Flutter(一)之Flutter的的简单入门分析
查看>>
【Vue源码学习】Virtual Dom 原理解析
查看>>
js 中有哪些拷贝的方式
查看>>
k8s简单了解
查看>>
Quartz学习-通过binlog分析Quartz启动及集群的Failover
查看>>
当下流行的UI框架
查看>>
Python从零开始系列连载(21)——Python特色数据类型(元组)(下)
查看>>
[掘金专题] Google I/O 2017 已经结束,我们该如何评价?
查看>>
深入剖析Vue源码 - 选项合并(下)
查看>>
vue父、子、孙组件间数据传递、事件传递
查看>>