Multithreading In Nodejs
Posted By : Shashwat Gupta | 12-Sep-2017
is it possible to achieve multithreading in nodejs?
Yes, multithreading is not the concept of core nodejs. you can achieve it in nodejs by using third party library. I'll explain you everything in detail below
What is Multi threading?
Multithreading is a process of executing multiple threads simultaneously. It's time saving process where You can perform many operations together it doesn't affect other thread because it is independent.
How To Achieve Multi-threading in NodeJs ?
You have to use async in your project for using it, you have run this commend it your project root path where package.json is available.
npm install --save async
after that write, this below code requires('async')
in top of your javascript file.
use async.parallel function for Achieve Multi threading in NodeJs
Syntax: parallel(tasks, callbackopt)
async.parallel({
First: function(callback) {
setTimeout(function() {
callback(null, "success");
}, 200);
},
Second: function(callback) {
setTimeout(function() {
callback(null, "success");
}, 100);
}
}, function(err, results)
{
// main results callback will go from here
});
Multithreading Advantages :
- Enhanced performance in less time
- Simplified program coding
- No dependency to other tasks
- Better cpu utilization
References : click here
you also can visit my youtube channel :
see you on next Blog
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shashwat Gupta
Shashwat is a bright Mean Stack Developer . He has good experience in development of complex UI's of web application and hybrid mobile applications.