欢迎大家来到IT世界,在知识的湖畔探索吧!
线程的创建
CBrother通过CreateThread方法创建多线程
函数 |
描述 |
用法 |
CreateThread(func,param) |
创建一个线程来执行func函数,并传递param给func函数,返回线程ID |
var threadid = CreateThread(MulThreadFunc,1) |
GetThreadID() |
获取当前线程ID |
var threadid = GetThreadID() |
function MulThreadFunc(param)
{
print "MulThreadFunc param:" + param " threadid:" + GetThreadID();
}
function main(param)
{
var threadid = CreateThread(MulThreadFunc,1); //第一个参数是函数,第二个参数是传递的参数
print "CreateThread threadid:" + threadid;
threadid = CreateThread(MulThreadFunc,2);
print "CreateThread threadid:" + threadid;
}
欢迎大家来到IT世界,在知识的湖畔探索吧!
结果:
欢迎大家来到IT世界,在知识的湖畔探索吧!CreateThread threadid:13241
CreateThread threadid:4637
MulThreadFunc param:1 threadid:13241
MulThreadFunc param:2 threadid:4637
线程同步 Lock
多线程同时修改同一个数据,只有一个线程会修改成功,其他线程将抛出SyncException
故多线程修改同一个数据的时候一定要用Lock类来做线程之间的同步
var myLock = new Lock();
函数 |
描述 |
lock |
加锁,其他线程再次加锁会阻塞 |
unlock |
解锁之后其他线程才可以获得锁权 |
例子:
欢迎大家来到IT世界,在知识的湖畔探索吧!var g_count = 0;
var g_lock = new Lock();
function MulThreadFunc(param)
{
for(var i = 0 ; i < 10 ; i++)
{
g_lock.lock();
g_count++;
print "MulThreadFunc param:" + param + " g_count:" + g_count;
g_lock.unlock();
}
}
function main(param)
{
CreateThread(MulThreadFunc,1);
CreateThread(MulThreadFunc,2);
while(g_count != 20)
{
Sleep(1000);
}
}
结果:
MulThreadFunc param:1 g_count:1
MulThreadFunc param:2 g_count:2
MulThreadFunc param:1 g_count:3
MulThreadFunc param:2 g_count:4
MulThreadFunc param:2 g_count:5
MulThreadFunc param:1 g_count:6
MulThreadFunc param:2 g_count:7
MulThreadFunc param:1 g_count:8
MulThreadFunc param:1 g_count:9
MulThreadFunc param:2 g_count:10
MulThreadFunc param:2 g_count:11
MulThreadFunc param:2 g_count:12
MulThreadFunc param:2 g_count:13
MulThreadFunc param:2 g_count:14
MulThreadFunc param:1 g_count:15
MulThreadFunc param:2 g_count:16
MulThreadFunc param:1 g_count:17
MulThreadFunc param:1 g_count:18
MulThreadFunc param:1 g_count:19
MulThreadFunc param:1 g_count:20
Thread 类
CBrother提供了Thread类,它有定时器功能,也可以处理线程消息
函数 |
描述 |
用法 |
setThreadAction(action) |
给thread添加一个action处理类对象,action类接口在下面讲 |
myThread.setThreadAction(action) |
addTimer(ms,func,cnt) |
添加一个心跳,每ms毫秒执行全局函数func,cnt为执行次数,cnt可以不传,缺省为-1,表示一直执行。线程启动前可以在外部调用,线程启动后只能在线程内部调用。 |
myThread.addTimer(1000,func); |
addMsg(msg) |
给线程发送一条消息,会在action的onMsg方法里被处理,msg可以为任意对象 |
myThread.addMsg(msg); |
start() |
启动线程 |
myThread.start(); |
stop() |
停止线程 |
myThread.stop(); |
thread对象销毁时,线程会停止。但如果thread对象被action对象引用,则线程不会停止。建议在不用线程的时候主动停止。
Thread响应Action类可以有如下接口
function onInit(),线程启动初始化。
function onEnd(),线程结束时。
function onMsg(msg),接收线程消息。
例子:
class ThreadMsg
{
var msgid;
}
class ThreadAction
{
var thread;
function ThreadAction(t)
{
thread = t;
this.thread.addTimer(1000,onHeart1);
this.thread.addTimer(1000,onHeart2,2);
}
function onInit()
{
print "thread init" ;
}
function onMsg(msg)
{
print "msg id:" + msg.msgid;
}
function onEnd()
{
print "thread end";
}
function onHeart1()
{
print "in action heart1";
}
function onHeart2()
{
print "in action heart2";
}
}
class testHeart
{
function onTestHeart()
{
print "in testHeart";
}
}
function TestTime()
{
print "in time1";
}
function TestTime2()
{
print "in time2";
}
function main(param)
{
var thread = new Thread();
thread.addTimer(1000,TestTime);
thread.addTimer(1000,TestTime2,2);
thread.addTimer(1000,new testHeart().onTestHeart);
thread.setThreadAction(new ThreadAction(thread));
thread.start();
var i = 0;
while(1)
{
Sleep(1000);
var msg = new ThreadMsg();
msg.msgid = i++;
thread.addMsg(msg);
if(i >= 5)
{
break;
}
}
thread.stop();
}
结果:
thread init
msg id:0
in time1
in time2
in testHeart
in action heart1
in action heart2
msg id:1
in time1
in time2
in testHeart
in action heart1
in action heart2
msg id:2
in time1
in testHeart
in action heart1
msg id:3
in time1
in testHeart
in action heart1
msg id:4
thread end
Event 类
多线程编程中,经常需要某个线程在等待其它线程做完一些处理后才能继续执行。CBrother提供了Event类来做这样的事情。
var myEvent = new Event();
函数 |
描述 |
用法 |
wait(outtime) |
开始等待,outtimeh毫秒后超时,不传递outtime默认为0,表示不超时。一个Event对象只能等待一次,多次调用返回false表示等待失败 |
myEvent.wait(); |
endWait(data) |
通知Event结束等待,data为传递给Event的数据,可以不传递。一个Event对象在wait之后之能触发一次endWait,多次调用返回false表示结束失败 |
myEvent.endWait(); |
isTimeOut() |
结束等待后用来判断是正常结束还是超时结束。等待没结束之前调用没有意义。 |
var isOut = myEvent.isTimeOut(); |
getData() |
等待结束后获取endWait传入的数据。等待没结束之前调用没有意义。每次endWaite后只能调用一次getData(),第二次调用返回null |
var data = myEvent.getData(); |
reset() |
重置事件对象,重置后又可以调用wait |
myEvent.reset(); |
例子:
var g_count = 0;
var g_lock = new Lock();
var g_Event = new Event();
function MulThreadFunc(param)
{
if(param == 1)
{
print "wait in thread:" + param;
g_Event.wait(3000); //等待3秒
print "wait end in thread:" + param;
}
for(var i = 0 ; i < 10 ; i++)
{
g_lock.lock();
g_count++;
print "MulThreadFunc param:" + param + " g_count:" + g_count;
g_lock.unlock();
}
if(g_Event.endWait())
{
print "endWait suc in thread:" + param;
}
else
{
print "endWait faile in thread:" + param;
}
}
function main(param)
{
CreateThread(MulThreadFunc,1);
CreateThread(MulThreadFunc,2);
while(g_count != 20)
{
Sleep(1000);
}
}
结果:
wait in thread:1
MulThreadFunc param:2 g_count:1
MulThreadFunc param:2 g_count:2
MulThreadFunc param:2 g_count:3
MulThreadFunc param:2 g_count:4
MulThreadFunc param:2 g_count:5
MulThreadFunc param:2 g_count:6
MulThreadFunc param:2 g_count:7
MulThreadFunc param:2 g_count:8
MulThreadFunc param:2 g_count:9
MulThreadFunc param:2 g_count:10
endWait suc in thread:2
wait end in thread:1
MulThreadFunc param:1 g_count:11
MulThreadFunc param:1 g_count:12
MulThreadFunc param:1 g_count:13
MulThreadFunc param:1 g_count:14
MulThreadFunc param:1 g_count:15
MulThreadFunc param:1 g_count:16
MulThreadFunc param:1 g_count:17
MulThreadFunc param:1 g_count:18
MulThreadFunc param:1 g_count:19
MulThreadFunc param:1 g_count:20
endWait faile in thread:1
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/36185.html