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. zImage.img、ramdisk.img、system.img、userdata.img介绍及解包、打包方法

    ramdisk.img system.img userdata.img介绍及解包.打包方法 Android 源码编译后,在out/target/product/generic下生成ramdisk.im ...

  2. DBCP连接Oracle,数据库重启后现OALL8 is in an inconsistent state异常

    最近,DBCP连接Oracle,数据库重启后现OALL8 is in an inconsistent state异常. 版本说明 commons-dbcp-1.4.jar commons-pool-1 ...

  3. 把int放在一个char数组里(用于处理每一位数字)

    sprintf(): #include <stdio.h> void put_int_with_space(int v) {     char str[50];    //定义一个足够大的 ...

  4. HDU 5640 King's Cake

    King's Cake Problem Description It is the king's birthday before the military parade . The ministers ...

  5. iOS深入学习(UITableView系列4:使用xib自定义cell)

    可以通过继承UITableViewCell重新自定义cell,可以像下面一样通过代码来自定义cell,但是手写代码总是很浪费时间, ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  6. How To Use RUN_PRODUCT In Oracle Forms

    Run_Product is used to run Oracle Reports (RDF/REP files) in Oracle Forms. It invokes one of the sup ...

  7. Action的搭建及application、request、Session的运用 多种方法

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  8. Shell Sort(草稿)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Shel ...

  9. python计算文件的行数和读取某一行内容的实现方法

    一.计算文件的行数 最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的需求了:count = len(op ...

  10. iOS - UISearchController

    前言 NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearch ...