Unity3d多线程

news/2024/7/5 1:42:07

为什么80%的码农都做不了架构师?>>>   hot3.png

(一)多线程的创建

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://my.oschina.net/igames/blog/619002


http://lihuaxi.xjx100.cn/news/285920.html

相关文章

第1关:利用栈实现整数的十进制转八进制

#ifndef stack__h #define stack__h#include <stdio.h> #include <stdlib.h>typedef int T; // 数据元素的数据类型struct Stack{T* data; // 数据元素存储空间的开始地址int top; // 栈顶的位置int max; // 栈的最大长度 };Stack* Stack_Create(int maxlen)…

数据可视化[python-pyecharts]制作中国各省份近三个月新型冠状病毒肺炎变化图

大体思路&#xff1a; 通过pyecharts等库一个for循环批量绘制近几个月每天的图&#xff0c;最后通过pr将图片合成 先看一下某一天的样图&#xff0c;用pr组合起来之后就是个动态的了 文章目录安装pyecharts库数据来源代码部分1.导入库2.将路径中所有文件找出保存至列表3.处理导…

树上启发式合并问题 ---- 2019icpc南昌 K. Tree (树上启发式合并 + 动态开点线段树)

题目链接 题目大意&#xff1a; 就是给你一颗树&#xff0c;每个点有个权值viv_ivi​&#xff0c;问你有多少对(x,y)(x,y)(x,y)满足&#xff1a; xxx不是yyy的祖先yyy也不是xxx的祖先xxx和yyy的距离不超过kkkxxx和yyy最近公共祖先&#xff1a;zzz,满足vxvy2vzv_xv_y2v_zvx​vy…

程序员最常说的9句话,精准!

1、别更新了学不动了。2、我不会修电脑&#xff0c;谢谢。3、听说今晚不用加班。4、是你的网络有问题。5、清一下缓存再试试6、扫码提需求&#xff0c;谢谢。7、换一台设备试试看。8、保证今晚十点上线。9、键盘给你&#xff0c;你来写。IT程序猿 微博网友评论&#xff1a;猫儿…

为何Google将几十亿行源代码放在一个仓库?

作者 | Rachel Potvin&#xff0c;Josh Levenberg 译者 | 张建军 编辑 | apddd 【AI科技大本营导读】与大多数开发者的想象不同&#xff0c;Google只有一个代码仓库——全公司使用不同语言编写的超过10亿文件&#xff0c;近百TB源代码都存放在自行开发的版本管理系统Piper中&…

7个Debug linux程序的Strace 列子

Strace是一个能帮助你解决问题的debugging工具 Strace监控指定程序系统调用和信号&#xff0c;在你没有源代码又想dubug程序的执行时是会用到的。Strace会以程序的开始到结束来顺序执行的 你可以从这个7个Strace 例子开始起步了解Strace 1.跟踪可执行程序的执行 你可以使用stra…

第2关:利用栈判断字符串括号是否匹配

#ifndef stack__h #define stack__h#include <stdio.h> #include <stdlib.h>typedef char T; // 数据元素的数据类型struct Stack{T* data; // 数据元素存储空间的开始地址int top; // 栈表的当前位置int max; // 栈表的最大长度 };Stack* Stack_Create(int …

Codeforces Round #645 (Div. 2)(D.The Best Vacation)

题目链接&#xff1a;https://codeforces.com/contest/1358/problem/D 思路&#xff1a;双指针前缀和 前缀和主要处理了两组数据&#xff1a;sum[]是某月到某月的天数,ans[] 代表某月到某月能得到得hug数 然后我们通过双指针的方法来遍历整个范围&#xff0c;当sum[r]-sum[l-1]…