JavaScript多线程 [英] JavaScript multithreading

查看:166
本文介绍了JavaScript多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on comparison for several different methods of implementing (real or fake) multithreading in JavaScript. As far as I know only webworkers and Google Gears WorkerPool can give you real threads (ie. spread across multiple processors with real parallel execution). I've found the following methods:


  • 使用 yield()

使用 setInterval()(或其他非阻塞函数)线程等待一个另一个

use setInterval() (or other non-blocking function) with threads waiting one for another

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

use Google Gears WorkerPool threads (with a plugin)

使用html5网络工作者

use html5 web workers

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

I read related questions and found several variations of the above methods, but most of those questions are old, so there might be a few new ideas.

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

I'm wondering - how else can you achieve multithreading in JavaScript? Any other important methods?

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

UPDATE: As pointed out in comments what I really meant was concurrency.

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

UPDATE 2: I found information that Silverlight + JScript supports multithreading, but I'm unable to verify this.

更新3 :谷歌弃用Gears: http:/ /code.google.com/apis/gears/api_workerpool.html

UPDATE 3: Google deprecated Gears: http://code.google.com/apis/gears/api_workerpool.html

推荐答案

网络工作者。它们是W3C标准(目前正在制定工作草案),并且不需要插件:

Web Workers. They’re a W3C standard (well, a working draft at the moment) for exactly this, and require no plugins:


此规范定义一个API,允许Web应用程序作者生成与其主页并行运行脚本的后台工作程序。

This specification defines an API that allows Web application authors to spawn background workers running scripts in parallel to their main page.

该规范还讨论了跨越工作者多核,用于真正的并发(这由浏览器的JavaScript引擎无形地处理):

The specification also discusses spreading workers across multiple cores, for true concurrency (this is handled invisibly by the browser’s JavaScript engine):


随着多核CPU的普及,一种获取方式更好的性能是在多个工人之间分配计算成本高昂的任务。在[one]示例中,对于每个从1到10,000,000的数字执行的计算成本高的任务是在10个子工作者中进行的。

With multicore CPUs becoming prevalent, one way to obtain better performance is to split computationally expensive tasks amongst multiple workers. In [one] example, a computationally expensive task that is to be performed for every number from 1 to 10,000,000 is farmed out to ten subworkers.

yield() setInterval()仅计划稍后发生的事情,它们不会与任何事物同时运行否则。

yield() and setInterval() only schedule things to happen later, they don’t run concurrently with anything else.

这篇关于JavaScript多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆