我正在使用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
我希望,它创建4个线程并行启动。 我怎样才能实现它?
答案 0 :(得分: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);