Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript多线程_Javascript_Multithreading_Google Gears_Web Worker - Fatal编程技术网

JavaScript多线程

JavaScript多线程,javascript,multithreading,google-gears,web-worker,Javascript,Multithreading,Google Gears,Web Worker,我正在比较几种在JavaScript中实现(真实或虚假)多线程的不同方法。据我所知,只有webworkers和Google Gears WorkerPool可以为您提供真正的线程(即,通过真正的并行执行跨多个处理器分布)。我发现了以下方法: 使用yield() 使用setInterval()(或其他非阻塞函数)使线程彼此等待 使用Google Gears WorkerPool线程(带有插件) 使用html5网络工作者 我阅读了相关的问题,发现了上述方法的几种变体,但大多数问题都是老问题,因此

我正在比较几种在JavaScript中实现(真实或虚假)多线程的不同方法。据我所知,只有webworkers和Google Gears WorkerPool可以为您提供真正的线程(即,通过真正的并行执行跨多个处理器分布)。我发现了以下方法:

  • 使用
    yield()

  • 使用
    setInterval()
    (或其他非阻塞函数)使线程彼此等待

  • 使用Google Gears WorkerPool线程(带有插件)

  • 使用html5网络工作者

我阅读了相关的问题,发现了上述方法的几种变体,但大多数问题都是老问题,因此可能会有一些新想法

我想知道-还有什么方法可以在JavaScript中实现多线程?还有其他重要的方法吗

更新:正如我在评论中指出的,我真正的意思是并发性

更新2:我找到了Silverlight+JScript支持多线程的信息,但我无法验证这一点


更新3:谷歌弃用的Gears:

JavaScript中不直接支持多线程。然而,你可以通过运用一些想法和方法来实现这一点

有以下几种方法:

var id = window.timeout("javascript code", time);
在这里,JavaScript代码在指定的时间后被调用,我们可以使用

window.clearTimeout(id);
为了清理。
通过这一点,我们可以实现伪并发。

q:在Javascript中还可以实现其他并发吗

您可以使用异步或“非阻塞”类型的方法。这是node.js系统的一个主要问题。 它不完全是多线程的,但它确实会更快。

。它们是W3C标准(目前是一个工作草案),不需要插件:

该规范定义了一个API,该API允许Web应用程序作者生成后台工作人员,并在其主页上并行运行脚本

该规范还讨论了在多个内核之间分配工作线程,以实现真正的并发性(这是由浏览器的JavaScript引擎无形地处理的):

随着多核CPU的普及,获得更好性能的一种方法是在多个工作者之间分配计算成本高昂的任务。在[一个]示例中,对于从1到10000000的每一个数字,要执行的计算开销很大的任务被分包给了十个子工作者

yield()
setInterval()
只计划以后发生的事情,它们不会与其他任何事情同时运行

我想知道-还有什么方法可以在JavaScript中实现多线程?还有其他重要的方法吗

您可以将代码转换为不具有任何显式循环或直接函数调用的JavaScript代码,而将代码划分为由线程引擎管理的小型执行单元。在我的示例代码中,我展示了如何转换带有循环的函数,但为了保持示例的简单性,我省略了函数调用的机制

转换过程基本上是通过在分割点拆分代码来实现的。这些划分点是函数调用和循环(如上所示)。在本例中,我使用了对象和键,但如果单元将堆栈存储为对象变量(即使用
this.foo=bar
而不是
stack[“foo”]=bar
),则在浏览器的JavaScript引擎上可能会容易得多

例如,以下代码:

// Phoney method purely to demonstrate structure
function Foo() {
  var i,
      sum = 0,
      accumulator_list = [],
      accumulator_modulus = [],
      kMaxAccumulatorCount = 100;

  // Calculate accumulations
  for(i = 0; i < kMaxAccumulatorCount; ++i) {
    current_accumulator = GetNextAccumulator()
    accumulator_list[i] = current_accumulator;
    sum = sum + current_accumulator;
  }

  // Calculate accumulator modulus
  for(i = 0; i < kMaxAccumulatorCount; ++i) {
    current_accumulator = accumulator_list[i];
    accumulator_modulus[i] = current_accumulator % kMaxAccumulatorCount;
  }
}
//仅用于演示结构的假方法
函数Foo(){
var i,
总和=0,
累加器列表=[],
累加器_模数=[],
kMaxAccumulatorCount=100;
//计算累积
对于(i=0;i
。。。变成这样:

function Foo_A(caller,stack) {
  var stack = {};
  stack["i"] = undefined;
  stack["sum"] = 0;
  stack["accumulator_list"] = [];
  stack["accumulator_modulus"] = [];
  stack["kMaxAccumulatorCount"] = 100;

  stack["i"] = 0;
  return {caller: caller, stack: stack, next=Foo_B};
}

function Foo_B(caller, stack) {
  stack["current_accumulator"] = GetNextAccumulator();
  stack["accumulator_list"][stack["i"]] = stack["current_accumulator"];
  stack["sum"] = stack["sum"] + stack["current_accumulator"];

  // For-loop condition satisfied ?
  if(stack["i"] < stack["kMaxAccumulatorCount"]) {
    ++stack["i"];
    return {caller: caller, stack: stack, next:Foo_B};
  } else {
    // Initialise the next for loop.
    stack["i"] = 0;
    return {caller: caller, stack: stack, next:Foo_C};
  }
}

function Foo_C(caller, stack) {
  stack["current_accumulator"] = stack["current_accumulator"][stack["i"]];
  stack["accumulator_modulus"][stack["i"]] = stack["current_accumulator"] % stack["kMaxAccumulatorCount"];

  // For-loop condition satisfied ?
  if(stack["i"] < stack["kMaxAccumulatorCount"]) {
    ++stack["i"];
    return {caller: caller, stack: stack, next:Foo_C};
  } else {
    // Function has finished so the next will be null. When the thread-engine sees this it simulates the behaviour of a return, pops its virtual stack and returns execution to the caller
    return {caller: caller, stack: stack, next:null};
  }
}
函数Foo_A(调用方、堆栈){
var stack={};
堆栈[“i”]=未定义;
堆栈[“和”]=0;
堆栈[“累加器列表”]=[];
堆栈[“累加器_模数”]=[];
堆栈[“kMaxAccumulatorCount”]=100;
堆栈[“i”]=0;
返回{caller:caller,stack:stack,next=Foo_B};
}
函数Foo_B(调用方、堆栈){
堆栈[“当前_累加器”]=GetNextacumerator();
堆栈[“累加器列表”][堆栈[“i”]=堆栈[“当前累加器”];
堆栈[“总和”]=堆栈[“总和”]+堆栈[“当前累加器”];
//是否满足循环条件?
if(堆栈[“i”]<堆栈[“kMaxAccumulatorCount”]){
++堆栈[“i”];
返回{caller:caller,stack:stack,next:Foo_B};
}否则{
//初始化下一个for循环。
堆栈[“i”]=0;
返回{caller:caller,stack:stack,next:Foo_C};
}
}
函数Foo_C(调用方、堆栈){
堆栈[“当前累加器”]=堆栈[“当前累加器”][堆栈[“i”];
堆栈[“累加器”][堆栈[“i”]=堆栈[“当前累加器”]%堆栈[“kMaxAccumulatorCount”];
//是否满足循环条件?
if(堆栈[“i”]<堆栈[“kMaxAccumulatorCount”]){
++堆栈[“i”];
返回{caller:caller,stack:stack,next:Foo_C};
}否则{
//函数已完成,因此下一个将为null。当线程引擎看到此情况时,它将模拟返回的行为,弹出其虚拟堆栈并将执行返回给调用方
返回{caller:caller,stack:stack,next:null};
}
}

是一个非常简单的JS多线程库,它包装了Web Workers,并为您完成了大部分工作。:)

我认为您的问题是,您还可以如何在Javascript中实现并发性。正如您所说的,调用yield()或使用setInterval()并不重要。此外,web Worker与本机操作系统线程也不完全一样