javascript - 事件驱动和并发问题?
你好,
我一直在研究javascript,nodejs。我不明白如何在 JavaScript 中避免并发问题。
假设我正在处理一个对象
var bigObject = new BigObject();
,并且我有一个 setTimer(function(){ workOnBigOjbect...} )
,它也可以在 bigOjbect
上工作。
如果我将磁盘 IO 写入 bigObject
,并且计时器对象在 bigObject
上工作,并定期从 bigObject
读取代码,并发问题如何避免?
在常规语言中,我会使用互斥体或线程安全队列/命令模式。我也没有看到太多关于 javascript 竞争条件的讨论。
我错过了什么吗?
Greetings,
I've been studying javascript, nodejs. And I don't understand how the concurrency issues are avoided in javascript.
Lets say I'm working on a object
var bigObject = new BigObject();
and I have a setTimer(function(){ workOnBigOjbect...} )
that will also do work on bigOjbect
.
If I have disk IO being written into bigObject
, and a timer object working on bigObject
, and regularly code reading from bigObject
, how are concurrency issues avoided?
In a regular language, I would use a mutex or thread-safe queue/command pattern. I also don't see much discussion about race conditions for javascript.
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Node.js 的重点在于它是事件驱动的。所有代码都在单个线程的事件处理程序中运行。不存在并发问题,因为代码不是同时运行的。缺点是每个事件处理程序必须快速退出,因为它会阻塞其他事件。
在您的示例中,代码将启动磁盘 IO 并立即退出。 Node.js 基础设施将通过运行事件处理程序来通知程序 IO 操作已完成。计时器事件将在 IO 事件之前或之后调用,但绝不会同时调用。
The whole point of node.js is that it's event-driven. All the code runs in event handlers in a single thread. There are no concurrency issues because the code doesn't run concurrently. The downside is that each event handler must exit quickly because it blocks the other events.
In your example, the code will start the disk IO and exit immediately. The node.js infrastructure will notify the program that the IO operation was completed by running an event handler. The timer event will be called before or after the IO event, but never concurrently.
JavaScript 是单线程的。如果函数应该执行的时间到了(基于调用 setTimer 的方式),并且父代码仍在运行,则函数将不会执行,直到父代码完成为止。
Javascript is single-threaded. If the time arrives when your function is supposed to execute (based on how you called setTimer), and the parent code is still running, the function will not execute until the parent code has completed.
只有一个线程;请参阅: 多核计算机上的 Node.js
我推测这是因为底层 V8 JavaScript 引擎不支持多线程,因为 JavaScript 通常在浏览器中执行(在 Windows 情况下只有一个 UI 线程)并且不支持多线程。
There is only a single thread; see: Node.js on multi-core machines
I would speculate that this is because Multiple threads are not supported in the underlying V8 JavaScript engine since typically JavaScript executes within a browser (where in a windows case there is only a single UI thread) and does not support multiple threads.
javascript 中有一个叫做 Run 的东西-to-Completion 确保如果代码正在执行,它会在任何其他(异步)代码运行之前完全执行,因此不存在并发问题。
在您的示例中,每当调用计时器回调时,它将完全执行,并且永远不会在中间被抢占以执行其他代码。
有关更多信息,请参阅为什么 javascript 中没有并发控制工具细节。
There's is this thing in javascript called Run-to-Completion which ensures that if a code is executing it executes completely before any other (asynchronous) code runs, hence, no concurrency issues.
In case of your example whenever the timer callback is called it will execute completely and will never be pre-empted in middle to execute some other code.
See Why no concurrency control tool in javascript for more details.