2015-04-25 15 views
1

我正在使用jxcore並行執行作業。但是當我測試如下Jxcore並行添加新任務

var method = function() { 
    var sleep = require('sleep'); 
    console.log("thread started", process.threadId); 
    sleep.sleep(2); 
    console.log("thread finished", process.threadId); 
}; 

jxcore.tasks.addTask(method); 
jxcore.tasks.addTask(method); 
jxcore.tasks.addTask(method); 
jxcore.tasks.addTask(method); 

結果似乎只有1線程用於:

thread started 0 
thread finished 0 
thread started 0 
thread finished 0 
thread started 0 
thread finished 0 
thread started 0 
thread finished 0 

我想到,它創建四線程並行啓動。 我該如何實現它?

+0

我不能重現此。你的操作系統是哪一個?你正在使用哪個jxcore版本? – infografnet

+0

我使用的是Ubuntu 14.04 lts。我認爲我在睡覺時錯了。它阻止了所有的線程。 – Thrall

回答

0

嘗試:

Function.prototype.clone = function() { 
    var that = this; 
    var temp = function temporary() { return that.apply(this, arguments); }; 
    for(var key in this) { 
     if (this.hasOwnProperty(key)) { 
      temp[key] = this[key]; 
     } 
    } 
    return temp; 
}; 

var method = function() { 
    var sleep = require('sleep'); 
    console.log("thread started", process.threadId); 
    sleep.sleep(2); 
    console.log("thread finished", process.threadId); 
}; 

jxcore.tasks.addTask(method); 
jxcore.tasks.addTask(method.clone()); 
jxcore.tasks.addTask(method.clone()); 
jxcore.tasks.addTask(method.clone()); 

或者:

var newmethod1 = method.clone(); 
jxcore.tasks.addTask(newmethod1); 
jxcore.tasks.runOnce(newmethod1); 
+1

sleep命令會中斷所有線程,所以我想我必須設置setTimeout來測試。 – Thrall