Reputation: 143
I don't understand what actually makes NodeJS seem single threaded? Is it the event loop that is single threaded or the fact that the actual JavaScript code is run in a single thread using the v8 engine? How do these two (v8 and event loop) interact with each other? And where can I see the place, in implementation code, these two meet?
Upvotes: 4
Views: 2580
Reputation: 409
The js code is executed as a single thread i.e you cannot create threads and execute two pieces of code in parallel in js. But the v8 engine can do 2 things parallaly like executing js code in one thread and waiting for i/o like network request in another thread which makes js asynchronous.
Upvotes: 0
Reputation: 6623
A small addition/clarification to the Node.js vs. V8 vs. Event Loop part of @Milan's answer : https://stackoverflow.com/a/50121471/1194266
V8's solution is to provide a default implementation that embedders can override
Upvotes: 0
Reputation: 2013
What I think your misunderstanding is - it’s that it is really single-threaded but it ‘seems multi-threaded’. How? - because of the Event Loop.
Because JavaScript is single-threaded Event Loop is what allows NodeJS to perform non-blocking I/O operations in background in “parallel”. NodeJS offloads these operations to the system kernel whenever possible (which nowadays most are multi-threaded)
Whenever one of these operation is finished - kernel signals to NodeJS to execute that operations callback function - adds that operation’s callback function to the poll queue to be executed in the poll phase of the Event Loop (explained below).
Upon start NodeJS initializes Event loop and starts processing. Event loop is comprised of sequence of processing phases, poll phase and it's queue being the one for I/O processing.
Each phase has a FIFO queue of callbacks to execute. While each phase is special in its own way, generally, when the event loop enters a given phase, it will perform any operations specific to that phase, then execute callbacks in that phase's queue until the queue has been exhausted or the maximum number of callbacks has executed. When the queue has been exhausted or the callback limit is reached, the event loop will move to the next phase, and so on. src
You can see more details on processing phases and Event Loop here: The Node.js Event Loop, Timers, and process.nextTick() | Node.js
Upvotes: 4
Reputation: 5425
Simplified explanation of why JavaScript is single-threaded:
The Event Loop continuously checks a queue to see if there is any code that needs to run.
while (queue.waitForMessage()) {
queue.processNextMessage();
}
queue.waitForMessage()
is synchronous so it waits for a new message to arrive if the queue is currently empty. In the JavaScript environment no function can be pre-empted - once a function starts to be executed it will run to completion before any other code is executed.
Node.js and V8
The same behaviour is found in every JavaScript environment and Node.js is just a JavaScript runtime built on Chrome's V8 JavaScript engine
The Node.js Event Loop
The Node.js Event Loop construct is more involved that what I outlined above. It is composed of several phases for handling FIFO queues for timers and callbacks and it is these elements that give the Node.js environment its asynchronous behaviour. All I/O operations are offloaded to the OS and because the underlying system is multi-threaded the operations are performed in the background. When the operation is completed the system notifies the Node.js process and a callback is added to the appropriate queue to await execution. A more complete description of the Node.js Event Loop is available in the guides section of the official Node.js website and the documentation of the libuv library which implements the asynchronous I/O.
Upvotes: 2
Reputation: 1776
Javascript is single threaded. That means at any time the cpu can only be executing one thing for the process ie the running app.
But javascript also has promises and async code. How is that possible if only one thing can execute at any time?
The answer is the event loop that tracks executions and runs the async work after the main work which simulates async.
I can't go into what the event loop looks like much as my own knowledge is not good there.
Upvotes: 0