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


当前回答

然而,你可以使用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;
    }
}

其他回答

目前一些浏览器确实支持多线程。如果你需要,你可以使用特定的库。例如,查看下面的材料:

https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers (支持后台线程); https://keithwhor.github.io/multithread.js/(图书馆)。

正如matt b所说,这个问题不是很清楚。假设您正在询问语言中的多线程支持:因为目前在浏览器中运行的99.999%的应用程序都不需要多线程。如果你真的需要它,有一些变通方法(比如使用window.setTimeout)。

一般来说,多线程是非常,非常,非常,非常,非常,非常,很难(我说过它很难吗?),除非你加入额外的限制(比如只使用不可变数据)。

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/

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

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

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

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

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

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

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