Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java NIO non-blocking mode vs node.js asynchronous operation

I have not gone through the code detail of node.js .

But, going through some research about thread in Node.js, I found that it has single thread for accepting connection from multiple clients.

When connected with client it fires connection events and listens for another client and work fully in asynchronous style and rest operation of client request is performed from thread pool and result is sent back to main thread(Thread that accepts connection) via callback.

Like wise in Java NIO also ServerSocketChannel,SocketChannel can be set in non-blocking mode and with selector single thread can monitor multiple channels. So, using NIO ServerSocketChannel,SocketChannel also from single thread the connection can be managed asynchronously for multiple clients

So, is the NIO's non-blocking mode and node.js asynchronous with single thread follows the same pattern for concept of single thread? As both say they perform on single thread.

like image 970
abishkar bhattarai Avatar asked Dec 23 '13 09:12

abishkar bhattarai


People also ask

What is the difference between asynchronous and non-blocking NodeJS?

Non-Blocking: It refers to the program that does not block the execution of further operations. Non-Blocking methods are executed asynchronously. Asynchronously means that the program may not necessarily execute line by line.

Is asynchronous same as non-blocking?

Asynchronous refers to something done in parallel, say is another thread. Non-blocking often refers to polling, i.e. checking whether given condition holds (socket is readable, device has more data, etc.)

Is node JS blocking or non-blocking?

All of the I/O methods in the Node. js standard library provide async versions, which are non-blocking, and accept callback functions.

Is NodeJS synchronous or asynchronous?

NodeJS is an asynchronous event-driven JavaScript runtime environment designed to build scalable network applications. Asynchronous here refers to all those functions in JavaScript that are processed in the background without blocking any other request.


1 Answers

Asynchrony in general, and NIO in particular, are not necessarily backed by single thread, they can be supported by multiple threads to increase performance. However, multithreading requires additional synchronization (not complex, but accurate). Since javascript lacks synchronization utilities, Node.js has to use single thread. Java asynchronous frameworks can use multiple threads.

Apendix

Why is Node.js single-threaded by design? From Understanding Node.js:

"So I don't have to worry about code accessing the same data structures at the same time?"

You got it! That's the entire beauty of JavaScripts single-threaded/event loop design!

So the most likely cause of single-threaded design is to please javascript programmers, which, en masse, are not familiar with synchronization concepts.

like image 52
Alexei Kaigorodov Avatar answered Sep 22 '22 13:09

Alexei Kaigorodov