我正在使用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);结果似乎只使用了一个线程:
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个线程并行启动。我怎样才能做到这一点?
发布于 2015-07-29 05:10:10
尝试:
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);https://stackoverflow.com/questions/29862585
复制相似问题