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 ...
随机推荐
- zImage.img、ramdisk.img、system.img、userdata.img介绍及解包、打包方法
ramdisk.img system.img userdata.img介绍及解包.打包方法 Android 源码编译后,在out/target/product/generic下生成ramdisk.im ...
- DBCP连接Oracle,数据库重启后现OALL8 is in an inconsistent state异常
最近,DBCP连接Oracle,数据库重启后现OALL8 is in an inconsistent state异常. 版本说明 commons-dbcp-1.4.jar commons-pool-1 ...
- 把int放在一个char数组里(用于处理每一位数字)
sprintf(): #include <stdio.h> void put_int_with_space(int v) { char str[50]; //定义一个足够大的 ...
- HDU 5640 King's Cake
King's Cake Problem Description It is the king's birthday before the military parade . The ministers ...
- iOS深入学习(UITableView系列4:使用xib自定义cell)
可以通过继承UITableViewCell重新自定义cell,可以像下面一样通过代码来自定义cell,但是手写代码总是很浪费时间, ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- 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 ...
- Action的搭建及application、request、Session的运用 多种方法
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Shell Sort(草稿)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Shel ...
- python计算文件的行数和读取某一行内容的实现方法
一.计算文件的行数 最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的需求了:count = len(op ...
- iOS - UISearchController
前言 NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearch ...