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
Я ожидаю, это создаст 4 потока запуска параллельно. Как я могу этого достичь?
1 ответ
Пытаться:
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);