这是一个深思熟虑的设计决定,还是我们当前浏览器的一个问题,这个问题将在未来的版本中得到纠正?
当前回答
然而,你可以使用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;
}
}
其他回答
它的实现不支持多线程。目前谷歌Gears提供了一种通过执行外部进程来使用某种形式的并发的方法,但仅此而已。
新的浏览器谷歌应该今天发布(谷歌Chrome)通过在进程中分离一些代码并行执行。
当然,核心语言可以提供与Java相同的支持,但对Erlang并发性之类的支持还远远不够。
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是在沙盒中运行的,以一种独立于机器/操作系统的方式运行,添加多线程支持会使语言变得复杂,并将语言与操作系统联系得太紧密。
实际上多线程与语言本身没有关系。 这是一个。net的多线程Javascript引擎。 它在多线程场景下工作得非常好。 它集成了c#运行时,所以所有的同步逻辑都类似于c#。可以启动/等待/等待任务,也可以启动线程。你甚至可以放锁。下面的示例演示了在。net运行时中使用Javascript语法的并行循环。
https://github.com/koculu/topaz
var engine = new TopazEngine();
engine.AddType(typeof(Console), "Console");
topazEngine.AddType(typeof(Parallel), "Parallel");
engine.ExecuteScript(@"
var sharedVariable = 0
function f1(i) {
sharedVariable = i
}
Parallel.For(0, 100000 , f1)
Console.WriteLine(`Final value: {sharedVariable}`);
");
除此之外,微软正在开发Napa.js,一个支持多线程的Node.js克隆。
https://github.com/microsoft/napajs
英特尔一直在做一些Javascript多线程的开源研究,最近在GDC 2012上展示了它。 这里是视频链接。研究小组使用的OpenCL主要集中在英特尔芯片组和Windows操作系统。该项目代号为RiverTrail,代码可在GitHub上获得
一些更有用的链接:
为Web应用程序构建计算高速公路