Getting Started With Node – Course

Multi-threading versus single-threading

Multi-threading approach provides parallelism using threads so that multiple programs can simultaneously run. With advantages come the problems too; it is really difficult to handle concurrency and deadlock in a multi-threading system.

On the other hand, with single-threading, there is no chance of deadlock in the process and managing the code is also easy. You can still hack and busy the event loop for no reason; however, that’s not the point.

Consider the following working diagram that is developed by StrongLoop—one of the core maintainers of Node.js:

Multithreading systems

Node.js uses single-threading for runtime environment; however, internally, it does create multiple threads for various I/O operations. It doesn’t imply that it creates threads for each connection, libuv contains the Portable Operating System Interface (POSIX) system calls for some I/O operations.

Multi-threading blocks the I/O until the particular thread completes its operation and results in overall slower performance. Consider the following image:

Single threading system

If the single-threading programs work correctly, they will never block the I/O and will be always ready to accept new connections and process them.

In the next lesson, we will learn how the event loop of Node works.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.