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;
多线程实例
- /*
- *
- * 游戏多线程
- * */
- 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();
- }
- }
- }
- }
注意:
1,当多个线程同时访问同一数据时要加线程锁lock。
- Object n=new Object();
- long shu = 0;
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void Update ()
- {
- lock (n)
- {
- xian = 1;
- }
- }
Unity3d多线程的更多相关文章
- unity3d多线程坑
单独起了一个线程来处理网络相关操作,比较常规的做法.本身没啥特别的东西,碰到了一个不大不小的坑折腾了好久,记录下来吧. 简单的说就是子线程中抛出的异常,如果没有catch的话,会导致子线程悄无声息的退 ...
- Unity3D 海水多线程渲染算法实现
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...
- [原]unity3d之http多线程异步资源下载
郑重声明:转载请注明出处 U_探索 本文诞生于乐元素面试过程,被面试官问到AssetBundle多线程异步下载时,愣了半天,同样也被深深的鄙视一回(做了3年多u3d 这个都没用过),所以发誓要实现出来 ...
- Loom工具类:Unity3D巧妙处理多线程
Loom代码不多,只有168行, 然而却具备了子线程运行Action, 子线程与主线程交互的能力! public static Thread RunAsync(Action a) public sta ...
- 【Unity3D基础教程】给初学者看的Unity教程(五):详解Unity3D中的协程(Coroutine)
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 为什么需要协程 在游戏中有许多过程(Proc ...
- Unity3D安卓打包参数配置与兼容性的关系分析
前言 在使用Unity3D工程导出安卓安装包的时候,往往会遇到兼容性的问题,针对某些机型,要么无法打开游戏,要么会出现卡机的现象.面对这种情况,我们可以调节相关的参数来提高兼容性. 为了了解在打包时候 ...
- Unity3D手游开发实践
<腾讯桌球:客户端总结> 本次分享总结,起源于腾讯桌球项目,但是不仅仅限于项目本身.虽然基于Unity3D,很多东西同样适用于Cocos.本文从以下10大点进行阐述: 架构设计 原生插件/ ...
- Unity3D重要知识点
数据结构和算法很重要!图形学也很重要!大的游戏公司很看重个人基础,综合能力小公司看你实际工作能力,看你的Demo. 1.什么是渲染管道? 是指在显示器上为了显示出图像而经过的一系列必要操作. 渲染管道 ...
- Unity3D 面试题汇总
最先执行的方法是: 1.(激活时的初始化代码)Awake,2.Start.3.Update[FixUpdate.LateUpdate].4.(渲染模块)OnGUI.5.再向后,就是卸载模块(TearD ...
随机推荐
- 2016年11月19日 星期六 --出埃及记 Exodus 20:10
2016年11月19日 星期六 --出埃及记 Exodus 20:10 but the seventh day is a Sabbath to the LORD your God. On it you ...
- webform数据导出
把数据放到一个泛型集合里,再把泛型集合里面的数据放到一个table中,设置好文件路径,然后进行文件读取,最后供用户下载. 数据导出放在一个按钮中就可以了 using System; using Sys ...
- 循环嵌套,while循环,穷举迭代循环
一.循环嵌套 简单的就是说,在一个for循环里嵌入多个小for循环. 其中,在打矩形.三角形和乘法口诀表之类的题目中,大for循环一般表示的是行数,其余的小for循环式每一行中的内容. 二.while ...
- OpenLayers学习记录(1)
1.部署自己的服务器 首先下载openlayers的源码.解压后里面有很多内容.我们只需要拷贝目录下的OpenLayer.js.根目录下的lib目录.根目录下的img目录 theme目录 到你网站的o ...
- Java 常用排序算法/程序员必须掌握的 8大排序算法
Java 常用排序算法/程序员必须掌握的 8大排序算法 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排序(直接选择排序.堆排序) 4)归并排序 5)分配 ...
- 【leetcode❤python】 374. Guess Number Higher or Lower
#-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return - ...
- UVA 11552 四 Fewest Flops
Fewest Flops Time Limit:2000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Statu ...
- git commit时message的问题
1: 在执行git commit的时候,有两种办法为该commit添加message信息一种是git commit -m 'your message'另一种是git commit会打开commit-e ...
- Date、String和Timestamp类型转换
1.String与Date类型转换: 1.获取当前系统时间: Date date1 = new Date(); //获取系统当前时间 Calendar cal = Calendar.getInst ...
- CSS笔记(十五)CSS3之用户界面
参考:http://www.w3school.com.cn/css3/css3_user_interface.asp 在 CSS3 中,新的用户界面特性包括重设元素尺寸.盒尺寸以及轮廓等. 新的用户界 ...