为什么不JavaScript支持多线程? [英] Why doesn't JavaScript support multithreading?

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

问题描述

它是一个经过深思熟虑的设计决策,或与我们当天的浏览器有问题,这将在未来的版本中解决?

Is it a deliberate design decision or a problem with our current day browsers which will be rectified in the coming versions?

推荐答案

JavaScript不支持多线程,因为在浏览器中的JavaScript之间的preTER是一个单独的线程(据我所知)。即使谷歌Chrome浏览器不会让一个网页的JavaScript同时运行,因为这会导致在现有的网页大规模的并发问题。所有Chrome确实是分开的多个组件(不同的标签,插件,等等)到独立的过程,但我不能想象有一个以上的线程JavaScript的一个页面。

JavaScript does not support multi-threading because the JavaScript interpreter in the browser is a single thread (AFAIK). Even Google Chrome will not let a single web page’s JavaScript run concurrently because this would cause massive concurrency issues in existing web pages. All Chrome does is separate multiple components (different tabs, plug-ins, etcetera) into separate processes, but I can’t imagine a single page having more than one JavaScript thread.

您可以使用,无论作为建议,使用的setTimeout 允许某种调度和假并发性。这将导致浏览器重新渲染线程的控制权,并开始给定的毫秒数后提供给的setTimeout JavaScript的code。如果你想允许视(你所看到的),同时在其上执行操作刷新这是非常有用的。刚刚经历例如循环协调并相应地更新一个元素只会让你看到的开始和结束位置,并没有在两者之间。

You can however use, as was suggested, use 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间preTER管理。这使得我们可以运行在下面的方式操作:

We use an abstraction library in JavaScript that allows us to create processes and threads which are all managed by the same JavaScript interpreter. This allows us to run actions in the following manner:


  • 处理A,线程1

  • 处理A,线程2

  • 工艺B,线程1

  • 处理A,线程3

  • 处理A,线程4

  • 工艺B,线程2

  • 暂停处理

  • 工艺B,线程3

  • 进程B,线程4

  • 工艺B,螺纹5

  • 开始处理

  • 处理A,螺纹5

这允许一些形式的调度和假货并行,启动和线程停止,等等,但它不会是真实的多线程。我不认为这永远不会在语言本身来实现的,因为真正的多线程是唯一有用的,如果浏览器可以运行一个单页多线程(或多个核心甚至更多),和困难有较大的方式比额外的可能性。

This allows some form of scheduling and fakes parallelism, starting and stopping of threads, etcetera, but it will not be true multi-threading. I don’t think it will ever be implemented in the language itself, since true multi-threading is only useful if the browser can run a single page multi-threaded (or even more than one core), and the difficulties there are way larger than the extra possibilities.

对于JavaScript的未来,看看这个:的http://开发商。 mozilla.org/$p$psentations/xtech2006/javascript/

For the future of JavaScript, check this out: http://developer.mozilla.org/presentations/xtech2006/javascript/

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

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