简体   繁体   中英

What is the difference between a blocking socket write and a non-blocking socket write in Java?

The difference between non-blocking reads and blocking reads is obvious, but I am confused about writes. I understand that a non-blocking write never blocks (duh!). If the underlying socket buffer is full, bytes are simply not written to it. That's why it is important to check how many bytes were written when you call channel.write.

Now how about blocking writes ? When does it block? When you call flush? When you call write on its OutputStream? Does it block until there is space in the underlying write socket buffer? Or does it throw an exception if it cannot write?

是的,它会阻塞,直到底层OS插槽发送缓冲区中有足够的空间来复制你的字节。当你将数据放入与套接字关联的Java OutputStream时,或者当你调用flush() ,可能会发生这种情况,无论哪个调用实际write(2)系统调用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM