Event Loop
JavaScript has a concurrency model based on an "event loop". The event loop got its name because of how it's usually implemented, which usually resembles:
while (queue.waitForMessage()) {
queue.processNextMessage();
}
queue.waitForMessage() waits synchronously for a message to arrive if there is none currently.
- Each message is processed completely before any other message is processed.
setTimeoutmessage: a minimum time and not a guaranteed time.- A web worker or a cross-origin
iframehas its own stack, heap, and message queue. - A very interesting property of the event loop model is that JavaScript, unlike a lot of other languages, never blocks. Handling I/O is typically performed via events and callbacks
Node JS Architecture – Single Threaded Event Loop
- The first basic thesis of node.js is that I/O is expensive.
- The second basic thesis is that thread-per-connection is memory-expensive.
- There is no way of making code run in parallel within a single request. However, all I/O is evented and asynchronous.
博客介绍了JavaScript基于事件循环的并发模型,事件循环会同步等待消息,且每条消息处理完才处理下一条,JavaScript不会阻塞,I/O通过事件和回调处理。还提到Node JS架构采用单线程事件循环,其认为I/O成本高,每个连接对应线程耗内存大,所有I/O是事件驱动且异步的。

被折叠的 条评论
为什么被折叠?



