AS3项目,AIR开发,使用多线程Worker,怎么让多个相同的子线程判断自己是线程池中的哪一个??
如题,假如我想使用4个相同的子线程,命名为worker1-worker4,这4个线程同时执行不同的任务。线程的创建和启动都没有问题,问题在于:如何让这4个子线程分别知道自己是哪一个线程?或者说自己在线程池中的名称/索引?不搞清这个问题,子线程就没法和主线程正确建立MessageChannel,也就没法通信。
我翻遍了所有官方的api,没找到任何有帮助的信息,也没找到相关的机制(也可能是我太菜),网上能搜到的所有方法也试过了,都失败了。
我的想法是在每个子线程内使用WorkerDomain.current.listWorkers()方法获取线程池,然后逐一判断池内某项是否等于Worker.current。看起来逻辑似乎合理,以下是我按照这个思路写的小样,运行起来虽然不报错,但也不正确。
有没有人能指出更好的思路?或者完善我的思路,Worker这玩意太难用了!
import flash.events.Event;
import flash.system.MessageChannel;
import flash.system.Worker;
import flash.system.WorkerDomain;
var curID: int = -1
var mainToWorker1: MessageChannel
var mainToWorker2: MessageChannel
var mainToWorker3: MessageChannel
var mainToWorker4: MessageChannel
var worker1ToMain: MessageChannel
var worker2ToMain: MessageChannel
var worker3ToMain: MessageChannel
var worker4ToMain: MessageChannel
if (Worker.current.isPrimordial) {
var worker1 = WorkerDomain.current.createWorker(this.loaderInfo.bytes, true);
var worker2 = WorkerDomain.current.createWorker(this.loaderInfo.bytes, true);
var worker3 = WorkerDomain.current.createWorker(this.loaderInfo.bytes, true);
var worker4 = WorkerDomain.current.createWorker(this.loaderInfo.bytes, true);
mainToWorker1 = Worker.current.createMessageChannel(worker1);
mainToWorker2 = Worker.current.createMessageChannel(worker2);
mainToWorker3 = Worker.current.createMessageChannel(worker3);
mainToWorker4 = Worker.current.createMessageChannel(worker4);
worker1ToMain = worker1.createMessageChannel(Worker.current);
worker2ToMain = worker2.createMessageChannel(Worker.current);
worker3ToMain = worker3.createMessageChannel(Worker.current);
worker4ToMain = worker4.createMessageChannel(Worker.current);
worker1.setSharedProperty("mainToWorker1", mainToWorker1);
worker2.setSharedProperty("mainToWorker2", mainToWorker2);
worker3.setSharedProperty("mainToWorker3", mainToWorker3);
worker4.setSharedProperty("mainToWorker4", mainToWorker4);
worker1.setSharedProperty("worker1ToMain", worker1ToMain);
worker2.setSharedProperty("worker2ToMain", worker2ToMain);
worker3.setSharedProperty("worker3ToMain", worker3ToMain);
worker4.setSharedProperty("worker4ToMain", worker4ToMain);
worker1ToMain.addEventListener(Event.CHANNEL_MESSAGE, fstep3);
worker2ToMain.addEventListener(Event.CHANNEL_MESSAGE, fstep3);
worker3ToMain.addEventListener(Event.CHANNEL_MESSAGE, fstep3);
worker4ToMain.addEventListener(Event.CHANNEL_MESSAGE, fstep3);
worker1.start();
worker2.start();
worker3.start();
worker4.start();
} else {
curID = getCurID()
this["mainToWorker" + curID] = Worker.current.getSharedProperty("mainToWorker" + curID);
this["worker" + curID + "ToMain"] = Worker.current.getSharedProperty("worker" + curID + "ToMain");
this["mainToWorker" + curID].addEventListener(Event.CHANNEL_MESSAGE, fstep2);
}
function getCurID() {
//这个获取ID的方式有没有问题?每个子线程中执行listWorkers()返回的结果是否一致?不知道
var v = WorkerDomain.current.listWorkers()
for (var a: int = 0; a < v.length; a++) {
if (v[a].isPrimordial) {
v.splice(a, 1)
break
}
}
for (var b: int = 0; b < v.length; b++) {
if (v[b] == Worker.current) {
return b + 1
}
}
}
//步骤1:点击屏幕发送初始消息
stage.addEventListener("click", f1)
function f1(e) {
txt1.appendText("\n初始消息已发送\n")
mainToWorker1.send("")
mainToWorker2.send("")
mainToWorker3.send("")
mainToWorker4.send("")
}
//步骤2:子线程收到消息,发送自己的编号
function fstep2(event: Event) {
this["worker" + curID + "ToMain"].send(curID)
}
//步骤3:主线程接收子线程编号
function fstep3(event: Event): void {
txt1.appendText("从分支线程发来的消息是\"" + event.target.receive() + "\"\n")
}