JavaScript / Web 中是否有任何同步原语,例如 Barriers、Semaphors、Locks、Monitors... Workers或者是否有一些可用的库使我能够使用这些东西(我正在考虑Java中的java.util.concurrent之类的东西)?
Workers 是否具有与线程不同的模糊属性(例如,它们可以与主线程共享内存)吗?是否有某种限制可以产生多少工作人员(例如,出于安全原因或其他原因......)?我需要特别注意什么吗?
Are there any synchronization primitives like Barriers, Semaphors, Locks, Monitors, ... available in JavaScript / Web Workers or is there some library available empowering me to make use of such things (I'm thinking of something like java.util.concurrent in Java)?
Do Workers have obscure properties which differentiate them from Threads (can they share memory with the main thread, for example)? Is there some kind of limit how many workers can be spawned (like, for security reasons or something...)? Do I have to take special care of something?
发布评论
评论(4)
Web Workers 没有共享内存的概念;线程之间传递的所有消息都会被复制。话虽如此,您没有屏障、信号量、锁和监视器,因为您在 Web Worker 模型中不需要它们。
共享内存的概念早在 2011 年 2 月就被提出,但由于开发人员的复杂性,现在的状态尚未修复 =>
https://lists.webkit.org/pipermail/webkit-unassigned /2011-February/287595.html
这里还有一个关于网络工作者的很好的简介。
http://blogs.msdn.com/b/ie/archive/2011/07/01/web-workers-in-ie10-background-javascript-makes-web-apps-faster.aspx< /a>
希望这有帮助
Web workers don't have a concept of shared memory; all messages that are passed between threads are copied. With that being said, you don't have Barriers, Semaphores, Locks, and Monitors, because you don't need them in the web worker model.
The concept of shared memory was proposed back in Feb 2011 but the status is now wontfix due to developer complexity =>
https://lists.webkit.org/pipermail/webkit-unassigned/2011-February/287595.html
There is also a nice blurb about web workers here.
http://blogs.msdn.com/b/ie/archive/2011/07/01/web-workers-in-ie10-background-javascript-makes-web-apps-faster.aspx
Hope this helps
简而言之:JavaScript 中没有任何同步原语,但也不需要它们,因为 JavaScript 本质上是单线程的:)。 Workers 只能访问自己的范围(没有 dom 操作,只有计算)并将消息发送到正常 js 所在的主 ui 线程。我不确定工作人员的最大数量,但肯定有限制,您可以在浏览器中尝试一下:)
希望这有帮助!
In short: no there aren't any synchronization primitives in javascript but there is also no need for them since JavaScript is inherently single threaded :). Workers can only access there own scope (no dom manipulation just calculations) and send messages to the main ui thread where the normal js resides. I'm not sure about the maximum count of workers but there sure is a limit, you could try it out in a browser :)
Hope this helps!
这里有一个基于 jQuery 的库,用于此目的: http://www.megiddo.ch /jcon-q-rency。
当然,该模型与 java.util.concurrent 并不完全相同,因为我们处理的环境不同,正如其他答案中所解释的......
Here you have a library based on jQuery made for that purpose: http://www.megiddo.ch/jcon-q-rency.
Of course the model is not really identical to java.util.concurrent since we are not dealing with the same environment, as explained in the other answers...
较新的答案:网络工作人员现在可以通过 SharedArrayBuffer 共享内存,尽管您的网站 需要配置跨域隔离才能使用它。但是,一旦您使用 SharedArrayBuffer,就可以使用
Atomics.wait()
和Atomics.notify()
获取互斥行为。但是,浏览器不允许从主线程调用
wait()
。主线程仍然只能与 Web 工作线程异步交互。Newer answer: web workers can share memory now via SharedArrayBuffer, although your site needs to be configured with cross-origin isolation in order to use this. But once you're using SharedArrayBuffer, it is possible to have one worker's execution synchronously block waiting on another worker thread, using
Atomics.wait()
andAtomics.notify()
to get mutex behaviors.Browsers don't allow calling
wait()
from the main thread, however. The main thread still can only interact with web worker threads asynchronously.