这是一个深思熟虑的设计决定,还是我们当前浏览器的一个问题,这个问题将在未来的版本中得到纠正?
当前回答
传统上,JS是为简短、快速运行的代码而设计的。如果你需要进行重要的计算,你就需要在服务器上进行——让一个JS+HTML应用程序长时间运行在你的浏览器中做一些重要的事情是荒谬的。
Of course, now we have that. But, it'll take a bit for browsers to catch up - most of them have been designed around a single-threaded model, and changing that is not easy. Google Gears side-steps a lot of potential problems by requiring that background execution is isolated - no changing the DOM (since that's not thread-safe), no accessing objects created by the main thread (ditto). While restrictive, this will likely be the most practical design for the near future, both because it simplifies the design of the browser, and because it reduces the risk involved in allowing inexperienced JS coders mess around with threads...
@marcio:
为什么不使用Javascript实现多线程呢?程序员可以用他们拥有的工具做任何他们想做的事情。
So then, let's not give them tools that are so easy to misuse that every other website i open ends up crashing my browser. A naive implementation of this would bring you straight into the territory that caused MS so many headaches during IE7 development: add-on authors played fast and loose with the threading model, resulting in hidden bugs that became evident when object lifecycles changed on the primary thread. BAD. If you're writing multi-threaded ActiveX add-ons for IE, i guess it comes with the territory; doesn't mean it needs to go any further than that.
其他回答
JavaScript不支持多线程,因为浏览器中的JavaScript解释器是一个单线程(AFAIK)。即使谷歌Chrome也不会让单个网页的JavaScript同时运行,因为这会导致现有网页的大量并发问题。Chrome所做的只是将多个组件(不同的选项卡、插件等)分离到单独的进程中,但我无法想象一个页面有多个JavaScript线程。
You can however use, as was suggested, setTimeout to allow some sort of scheduling and “fake” concurrency. This causes the browser to regain control of the rendering thread, and start the JavaScript code supplied to setTimeout after the given number of milliseconds. This is very useful if you want to allow the viewport (what you see) to refresh while performing operations on it. Just looping through e.g. coordinates and updating an element accordingly will just let you see the start and end positions, and nothing in between.
我们在JavaScript中使用了一个抽象库,它允许我们创建进程和线程,它们都由同一个JavaScript解释器管理。这允许我们以以下方式运行操作:
进程A,线程1 进程A,线程2 进程B,线程1 进程A,线程3 进程A,线程4 进程B,线程2 暂停进程A 进程B,线程3 进程B,线程4 进程B,线程5 启动流程A 进程A,线程5
这允许某种形式的调度和伪并行,线程的启动和停止等等,但它不是真正的多线程。我不认为它会在语言本身中实现,因为真正的多线程只有在浏览器可以运行单页多线程(甚至不止一个核心)时才有用,而且那里的困难比额外的可能性要大得多。
关于JavaScript的未来,看看这个: https://developer.mozilla.org/presentations/xtech2006/javascript/
你的意思是为什么语言不支持多线程,或者为什么浏览器中的JavaScript引擎不支持多线程?
第一个问题的答案是,浏览器中的JavaScript是在沙盒中运行的,以一种独立于机器/操作系统的方式运行,添加多线程支持会使语言变得复杂,并将语言与操作系统联系得太紧密。
据我所知,谷歌Chrome将有多线程javascript,所以这是一个“当前实现”的问题。
JavaScript多线程(有一些限制)在这里。谷歌实现了Gears的worker, HTML5也包含了worker。大多数浏览器已经添加了对该特性的支持。
数据的线程安全得到了保证,因为所有与worker通信的数据都被序列化/复制。
欲了解更多信息,请阅读:
http://www.whatwg.org/specs/web-workers/current-work/
http://ejohn.org/blog/web-workers/
传统上,JS是为简短、快速运行的代码而设计的。如果你需要进行重要的计算,你就需要在服务器上进行——让一个JS+HTML应用程序长时间运行在你的浏览器中做一些重要的事情是荒谬的。
Of course, now we have that. But, it'll take a bit for browsers to catch up - most of them have been designed around a single-threaded model, and changing that is not easy. Google Gears side-steps a lot of potential problems by requiring that background execution is isolated - no changing the DOM (since that's not thread-safe), no accessing objects created by the main thread (ditto). While restrictive, this will likely be the most practical design for the near future, both because it simplifies the design of the browser, and because it reduces the risk involved in allowing inexperienced JS coders mess around with threads...
@marcio:
为什么不使用Javascript实现多线程呢?程序员可以用他们拥有的工具做任何他们想做的事情。
So then, let's not give them tools that are so easy to misuse that every other website i open ends up crashing my browser. A naive implementation of this would bring you straight into the territory that caused MS so many headaches during IE7 development: add-on authors played fast and loose with the threading model, resulting in hidden bugs that became evident when object lifecycles changed on the primary thread. BAD. If you're writing multi-threaded ActiveX add-ons for IE, i guess it comes with the territory; doesn't mean it needs to go any further than that.
推荐文章
- 克隆对象没有引用javascript
- 验证字符串是否为正整数
- 如何获得一个键/值JavaScript对象的键
- 什么时候JavaScript是同步的?
- 如何在Typescript中解析JSON字符串
- Javascript reduce()在对象
- 在angularJS中& vs @和=的区别是什么
- 错误"Uncaught SyntaxError:意外的标记与JSON.parse"
- JavaScript中的querySelector和querySelectorAll vs getElementsByClassName和getElementById
- 给一个数字加上st, nd, rd和th(序数)后缀
- 如何以编程方式触发引导模式?
- setTimeout带引号和不带括号的区别
- 在JS的Chrome CPU配置文件中,'self'和'total'之间的差异
- 用javascript检查输入字符串中是否包含数字
- 如何使用JavaScript分割逗号分隔字符串?