19

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
abishkar bhattarai
  • 7,371
  • 8
  • 49
  • 66
  • 1
    It is common misconception to assume async and non-blocking are similar patterns. Node.js also has non-blocking socket operations. So yes, both frameworks have non-blocking IO allowing them to handle multiple several conenctions from single thread. – user568109 Dec 23 '13 at 11:19

2 Answers2

5

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.

Community
  • 1
  • 1
Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • 2
    Node.js uses single thread, so it doesn't need synchronization primitives, not the other way around – vkurchatkin Dec 23 '13 at 10:53
  • 1
    you said the reason node.js is singlethreaded is that javascript lacks synchronization utilities. That's not true — node.js is singlethreaded by design, so it doesn't NEED them. If they were needed, they could be implemented, there is nothing about javascript that can prevent it – vkurchatkin Dec 23 '13 at 11:20
3

No. Non-blocking means that the operations don't block, and they tell you what they did. Asynchronous means they the operations continue in parallel and call you back when they finish. They are completely different programming paradigms.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I wrote this question http://stackoverflow.com/questions/35660710/if-a-web-server-is-non-blocking-does-this-mean-it-is-handling-io-the-same-as-no and wondered if you think I am confusing the same things. – johnny Feb 26 '16 at 21:53
  • @johhny Yes you are. – user207421 Feb 26 '16 at 23:54