多线程任务分派及锁的简单例子
import win.ui;
import thread.works;
io.open();
var works = thread.works( 10,
function(...) {
thread.lock("写控制台")
io.print("线程ID" + thread.getId(),",开始工作,接收到任务指令参数",...)
thread.unlock("写控制台")
sleep(2000)
var abc=..time(,"%Y-%m-%d %H:%M:%S");
return { ... ;time = abc;str = "线程ID" + thread.getId()};
}
);
//分派任务
works.push("一个任务")
works.push("两个任务")
works.push("三个任务")
works.push("4个任务")
works.push("5个任务")
works.push("6个任务")
works.push("7个任务")
while( works.busy() ) {
var ret = works.pop(1) ;
if( #ret ){
io.print("检查成果", ret[1], ret.time, ret.str )
}
}
var ret = { works.pop(10) };
if( #ret ){
for(k,v in ret){
io.print("检查成果", v[1], v.time, v.str )
}
}
works.quit();
io.print("演示结束,按任意键退出...");
execute("pause > nul");
io.close();