且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java中的非阻塞套接字写入与阻塞套接字写入相比

更新时间:2022-06-10 21:56:33


我的理解是,如果你想确保另一方得到了返回写入方法后的TCP数据包

My understanding is that you would only want blocking write if you want to make sure the other side got the TCP packet once the write method returned

您的理解不正确。它不能确保。

Your understanding is incorrect. It doesn't ensure that.

阻塞写入阻塞,直到所有数据都被传输到套接字发送缓冲区,从那里异步传输到网络。如果读取器很慢,他的套接字接收缓冲区将填满,这最终会导致套接字发送缓冲区填满,这将导致阻塞写入阻塞,阻塞整个线程。非阻塞I / O为您提供了一种检测和处理这种情况的方法。

Blocking writes block until all the data has been transferred to the socket send buffer, from where it is transferred asynchronously to the network. If the reader is slow, his socket receive buffer will fill up, which will eventually cause your socket send buffer to fill up, which will cause a blocking write to block, blocking the whole thread. Non-blocking I/O gives you a way to detect and handle that situation.