3
votes

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?

1
and there's also async write...irreputable
@irreputable async write is non-blocking write I would think.chrisapotek
not precisely. these 3 modes are quite different from each other.irreputable
@irreputable What is the difference between an async write and a non-blocking write? Does the async write gives you a callback somehow? I have never seen this in Java. Do you have a link with an example?chrisapotek

1 Answers

2
votes

Yes, it does block until there's enough space in the underlying OS socket send buffer to copy your bytes in. That might happen while you are putting data into Java OutputStream associated with the socket, or when you call flush(), whichever invokes actual write(2) system call.