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


当前回答

传统上,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多线程(有一些限制)在这里。谷歌实现了Gears的worker, HTML5也包含了worker。大多数浏览器已经添加了对该特性的支持。

数据的线程安全得到了保证,因为所有与worker通信的数据都被序列化/复制。

欲了解更多信息,请阅读:

http://www.whatwg.org/specs/web-workers/current-work/

http://ejohn.org/blog/web-workers/

我不知道这个决定的基本原理,但我知道可以使用setTimeout模拟多线程编程的一些好处。您可以给人一种错觉,认为多个进程同时在做事情,但实际上,所有事情都发生在一个线程中。

只需要让你的函数做一点工作,然后调用类似这样的东西:

setTimeout(function () {
    ... do the rest of the work...
}, 0);

任何其他需要做的事情(如UI更新,动画图像等)都会在他们有机会的时候发生。

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

一些更有用的链接:

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

然而,你可以使用eval函数在一定程度上带来并发性

/* content of the threads to be run */
var threads = [
        [
            "document.write('Foo <br/>');",
            "document.write('Foo <br/>');",
            "document.write('Foo <br/>');",
            "document.write('Foo <br/>');",
            "document.write('Foo <br/>');",
            "document.write('Foo <br/>');",
            "document.write('Foo <br/>');",
            "document.write('Foo <br/>');",
            "document.write('Foo <br/>');",
            "document.write('Foo <br/>');"
        ],
        [
            "document.write('Bar <br/>');",
            "document.write('Bar <br/>');",
            "document.write('Bar <br/>');",
            "document.write('Bar <br/>');",
            "document.write('Bar <br/>');",
            "document.write('Bar <br/>');",
            "document.write('Bar <br/>');",
            "document.write('Bar <br/>');",
            "document.write('Bar <br/>');"
        ]
    ];

window.onload = function() {
    var lines = 0, quantum = 3, max = 0;

    /* get the longer thread length */
    for(var i=0; i<threads.length; i++) {
        if(max < threads[i].length) {
            max = threads[i].length;
        }
    }

    /* execute them */
    while(lines < max) {
        for(var i=0; i<threads.length; i++) {
            for(var j = lines; j < threads[i].length && j < (lines + quantum); j++) {
                eval(threads[i][j]);
            }
        }
        lines += quantum;
    }
}

据我所知,谷歌Chrome将有多线程javascript,所以这是一个“当前实现”的问题。