【问题标题】:Jxcore add new task parallelJxcore 并行添加新任务
【发布时间】:2015-07-03 22:46:31
【问题描述】:

我正在使用 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 个线程并行启动。 我怎样才能实现它?

【问题讨论】:

  • 我无法重现此内容。你的操作系统是哪个?您使用的是哪个 jxcore 版本?
  • 我使用的是 ubuntu 14.04 lts。我认为我在使用睡眠时错了。它阻塞了所有线程。

标签: jxcore


【解决方案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);

【讨论】:

  • sleep 命令会中断所有线程,所以我想我必须 setTimeout 来测试。
猜你喜欢
  • 2015-06-10
  • 2015-12-19
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
  • 2011-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多