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;

多线程实例

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

注意:

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

    1. Object n=new Object();
    2. long shu = 0;
    3. // Use this for initialization
    4. void Start () {
    5. }
    6. // Update is called once per frame
    7. void Update ()
    8. {
    9. lock (n)
    10. {
    11. xian = 1;
    12. }
    13. }

Unity3d多线程的更多相关文章

  1. unity3d多线程坑

    单独起了一个线程来处理网络相关操作,比较常规的做法.本身没啥特别的东西,碰到了一个不大不小的坑折腾了好久,记录下来吧. 简单的说就是子线程中抛出的异常,如果没有catch的话,会导致子线程悄无声息的退 ...

  2. Unity3D 海水多线程渲染算法实现

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...

  3. [原]unity3d之http多线程异步资源下载

    郑重声明:转载请注明出处 U_探索 本文诞生于乐元素面试过程,被面试官问到AssetBundle多线程异步下载时,愣了半天,同样也被深深的鄙视一回(做了3年多u3d 这个都没用过),所以发誓要实现出来 ...

  4. Loom工具类:Unity3D巧妙处理多线程

    Loom代码不多,只有168行, 然而却具备了子线程运行Action, 子线程与主线程交互的能力! public static Thread RunAsync(Action a) public sta ...

  5. 【Unity3D基础教程】给初学者看的Unity教程(五):详解Unity3D中的协程(Coroutine)

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 为什么需要协程 在游戏中有许多过程(Proc ...

  6. Unity3D安卓打包参数配置与兼容性的关系分析

    前言 在使用Unity3D工程导出安卓安装包的时候,往往会遇到兼容性的问题,针对某些机型,要么无法打开游戏,要么会出现卡机的现象.面对这种情况,我们可以调节相关的参数来提高兼容性. 为了了解在打包时候 ...

  7. Unity3D手游开发实践

    <腾讯桌球:客户端总结> 本次分享总结,起源于腾讯桌球项目,但是不仅仅限于项目本身.虽然基于Unity3D,很多东西同样适用于Cocos.本文从以下10大点进行阐述: 架构设计 原生插件/ ...

  8. Unity3D重要知识点

    数据结构和算法很重要!图形学也很重要!大的游戏公司很看重个人基础,综合能力小公司看你实际工作能力,看你的Demo. 1.什么是渲染管道? 是指在显示器上为了显示出图像而经过的一系列必要操作. 渲染管道 ...

  9. Unity3D 面试题汇总

    最先执行的方法是: 1.(激活时的初始化代码)Awake,2.Start.3.Update[FixUpdate.LateUpdate].4.(渲染模块)OnGUI.5.再向后,就是卸载模块(TearD ...

随机推荐

  1. 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 ...

  2. webform数据导出

    把数据放到一个泛型集合里,再把泛型集合里面的数据放到一个table中,设置好文件路径,然后进行文件读取,最后供用户下载. 数据导出放在一个按钮中就可以了 using System; using Sys ...

  3. 循环嵌套,while循环,穷举迭代循环

    一.循环嵌套 简单的就是说,在一个for循环里嵌入多个小for循环. 其中,在打矩形.三角形和乘法口诀表之类的题目中,大for循环一般表示的是行数,其余的小for循环式每一行中的内容. 二.while ...

  4. OpenLayers学习记录(1)

    1.部署自己的服务器 首先下载openlayers的源码.解压后里面有很多内容.我们只需要拷贝目录下的OpenLayer.js.根目录下的lib目录.根目录下的img目录 theme目录 到你网站的o ...

  5. Java 常用排序算法/程序员必须掌握的 8大排序算法

    Java 常用排序算法/程序员必须掌握的 8大排序算法 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排序(直接选择排序.堆排序) 4)归并排序 5)分配 ...

  6. 【leetcode❤python】 374. Guess Number Higher or Lower

    #-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return - ...

  7. UVA 11552 四 Fewest Flops

    Fewest Flops Time Limit:2000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Statu ...

  8. git commit时message的问题

    1: 在执行git commit的时候,有两种办法为该commit添加message信息一种是git commit -m 'your message'另一种是git commit会打开commit-e ...

  9. Date、String和Timestamp类型转换

    1.String与Date类型转换: 1.获取当前系统时间: Date date1 = new Date();   //获取系统当前时间 Calendar cal = Calendar.getInst ...

  10. CSS笔记(十五)CSS3之用户界面

    参考:http://www.w3school.com.cn/css3/css3_user_interface.asp 在 CSS3 中,新的用户界面特性包括重设元素尺寸.盒尺寸以及轮廓等. 新的用户界 ...