java nio node,Java的NIO非阻塞模式VS Node.js的异步操作

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.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值