Open In App

If Node.js is single threaded then how to handles concurrency ?

Last Updated : 16 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Node js is an open-source virtual machine that uses javascript as its scripting language. Despite being single-threaded, it is one of the most popular web technologies. The reason why node js is popular despite being single-threaded is the asynchronous nature that makes it possible to handle concurrency and perform multiple I/O operations at the same time. Node js uses an event loop to maintain concurrency and perform non-blocking I/O operations.

As soon as Node js starts, it initializes an event loop. The event loop works on a queue (which is called an event queue) and performs tasks in FIFO(First In First Out) order. It executes a task only when there is no ongoing task in the call stack. The call stack works in LIFO(Last In First Out) order. The event loop continuously checks the call stack to check if there is any task that needs to be run. Now whenever the event loop finds any function, it adds it to the stack and runs in order.  

Let’s see an example to get a proper understanding of how to call stack and event loop works and handles concurrency,

Example 1: In this example, we will see the working of the call stack.

Javascript




<script>
function add(a,b){
   return a+b;
}
function print(n){
   console.log(`Two times the number ${n} is `+add(n,n));
}
  
print(5);
</script>


Output:

Two times the number 5 is 10

Explanation: Here, when the code executes, the function print(5) will be invoked and will push into the call stack. When the function is called, it starts consoling the statement inside it but before consoling the whole statement it encounters another function add(n,n) and suspends its current execution, and pushes the add function into the top of the call stack. Now the function will return the addition a+b and then popped out from the stack and now the previously suspended function will start running and will log the output to console and then this function too will get pop from the stack and now the stack is empty. So this is how a call stack works. 

Example 2: In this example, we will see the Working of the Event Loop.

Javascript




<script>
function func(){
    console.log("Statement 1 inside function");
    setTimeout(function(){
        console.log("Statement 2 inside function");
    },2000);
    console.log("Statement 3 inside function");
      
}
console.log("Statement outside function");
func();
</script>


Output: 

Statement outside function
Statement 1 inside function
Statement 3 inside function
Statement 2 inside function

Explanation: In the example above. the first console statement “statement outside function” will be pushed into the stack and get logged into the console and popped off the stack. Now the function func() will be invoked and pushed into the stack. Then the statement 1 “Statement 1 inside function” will be pushed and logged console and popped out of the stack. Then the runtime goes to setTimeout() function and because this is a web API it goes to the browser now the browser will set the timer and execute it and then it goes popped off the stack and meanwhile next statement makes its way to call stack and therefore “Statement 3 inside the function” will get printed to console. Now when 2 seconds(2000ms) are completed browser will push the function back to the event queue. Now event loop checks whether the call stack is empty or not then pushes the function into the call stack and “Statement 2 inside the function” will get printed to the console and the function will get popped out of the stack. Now the function is completed and there are no more events left. 

This is how concurrency can be achieved by using an event loop and call stack.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads