javascript nodejs中的并发模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5153492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Models of concurrency in nodejs
提问by OrangeDog
Does anyone know what the memory and threading models are in nodejs?
有谁知道 nodejs 中的内存和线程模型是什么?
In particular, is ii++atomic? Does it behave as if iiwere volatilein Java 1.5, in Java 1.4, in C, or not at all?
特别是,是ii++原子的?它是否表现得好像ii是volatile在Java 1.5中,在Java 1.4中,在C,或者根本没有?
回答by Matt Ranney
It is useful to understand how node and V8 interact. Node handles waiting for I/O or timers from the operating system. When node wakes up from I/O or a timer, it generally has some JavaScript callbacks to invoke. When node runs these callbacks, control is passed into V8 until V8 returns back to node.
了解 node 和 V8 如何交互很有用。节点处理等待来自操作系统的 I/O 或计时器。当节点从 I/O 或计时器唤醒时,它通常会调用一些 JavaScript 回调。当 node 运行这些回调时,控制权会传递给 V8,直到 V8 返回给 node。
So, if you do var ii = 1; ii++;, you will never find that ii is anything other than 2. All JavaScript runs until completion, and then control is passed back to node. If you do doSomething(); doSomething();that will always run doSomething twice, and it will not return to node's event loop until the second invocation of doSomething returns. This means you can completely lock up node from a simple error like this:
所以,如果你这样做了var ii = 1; ii++;,你将永远不会发现 ii 是 2 以外的任何东西。所有的 JavaScript 都会运行直到完成,然后控制权被传递回节点。如果这样做doSomething(); doSomething();,将始终运行 doSomething 两次,并且在第二次调用 doSomething 返回之前它不会返回到节点的事件循环。这意味着您可以从一个简单的错误中完全锁定节点,如下所示:
for (var i=0 ; i >= 0 ; i++) {}
It doesn't mater how many I/O callbacks you have registered, timers set to go off, or sockets waiting to be read. Until V8 returns from that infinite loop, node does no more work.
您注册了多少 I/O 回调、设置为关闭的计时器或等待读取的套接字都无关紧要。直到 V8 从那个无限循环中返回,node 才不再工作。
This is part of what makes programming in node so nice. You never have to worry about locking. There are no race conditions or critical sections. There is only one thread where your JavaScript code runs.
这是使 node 中的编程如此出色的部分原因。您永远不必担心锁定。没有竞争条件或临界区。只有一个线程运行您的 JavaScript 代码。
回答by alienhard
There's only one thread (the event loop) and your code is never interrupted unless you perform an asynchronous action like I/O. You can't do any parallel code execution. Therefore ii++ is atomic.
只有一个线程(事件循环)并且您的代码永远不会被中断,除非您执行诸如 I/O 之类的异步操作。您无法执行任何并行代码执行。因此ii++ 是原子的。
回答by Michael Dillon
A good article that explains what is, and is not, asynchronous in node.js is Understanding the node.js Event Loop. If you can understand that you will be able to identify where your application has async behavior and where it doesn't. By understanding this you can explicitly write sequential code when you need it. EventEmitters are key.
一篇解释 node.js 中异步和非异步的好文章是了解 node.js 事件循环。如果您能理解,您将能够确定您的应用程序在哪些地方具有异步行为,哪些地方没有。通过理解这一点,您可以在需要时明确地编写顺序代码。EventEmitters 是关键。
Singlethreadedness sounds at odds with the idea that node.js is high performance and scalable so have a look at this article from Yahoo on Multicore.
单线程听起来与 node.js 具有高性能和可扩展性的想法不一致,因此请查看Yahoo on Multicore上的这篇文章。

