这是一个深思熟虑的设计决定,还是我们当前浏览器的一个问题,这个问题将在未来的版本中得到纠正?


当前回答

如果没有适当的语言支持线程同步,那么新的实现尝试线程同步就没有意义了。现有的复杂JS应用(例如任何使用ExtJS的应用)很可能会意外崩溃,但如果没有同步关键字或类似的东西,也很难甚至不可能编写正确运行的新程序。

其他回答

Javascript是一种单线程语言。这意味着它有一个调用堆栈和一个内存堆。正如预期的那样,它按顺序执行代码,并且必须在执行下一段代码之前完成。它是同步的,但有时是有害的。例如,如果一个函数需要一段时间才能执行,或者必须等待某项操作,那么它会同时冻结所有内容。

如果没有适当的语言支持线程同步,那么新的实现尝试线程同步就没有意义了。现有的复杂JS应用(例如任何使用ExtJS的应用)很可能会意外崩溃,但如果没有同步关键字或类似的东西,也很难甚至不可能编写正确运行的新程序。

传统上,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多线程的开源研究,最近在GDC 2012上展示了它。 这里是视频链接。研究小组使用的OpenCL主要集中在英特尔芯片组和Windows操作系统。该项目代号为RiverTrail,代码可在GitHub上获得

一些更有用的链接:

为Web应用程序构建计算高速公路

javascript多线程显然是可能的使用HTML5带来的网络工作者。

webworker和标准多线程环境之间的主要区别是内存资源不与主线程共享,对象的引用从一个线程到另一个线程是不可见的。线程通过交换消息进行通信,因此可以实现遵循事件驱动设计模式的同步和并发方法调用算法。

有很多框架允许在线程之间结构化编程,其中包括OODK-JS,这是一个支持并发编程的OOP js框架 https://github.com/GOMServices/oodk-js-oop-for-js