AsynchronousSocketChannel write() semantics

Alan Bateman Alan.Bateman at Sun.COM
Fri Jan 22 09:52:19 PST 2010


On 21 Jan 2010, at 04:01, Avinash Lakshman wrote:

> Hi All
>
> I am trying to understand the semantics of the write() in the  
> AsynchronousSocketChannel (ASC). Here are my questions:
>
> (1)  Is will the completion handler invoked only when the write  
> completes i.e all the bytes in the byte buffer are written out?

No, if there are N bytes remaining, then it writes up to N bytes, and  
the result of the operation (notified to the completion handler or  
returned by the Future's get method) is the number of bytes written.  
Hopefully this is clear from the spec.


> (2) Suppose two threads write to the same ASC instance will the  
> second write fail if the first has not completed? What if all the  
> bytes of the first write have not been written out? How do I handle  
> the subsequent write? Are the semantics same as in non-blocking I/O?

AsynchronousSocketWrite.write fails with a WritePendingException if a  
previous write hasn't completed. It doesn't matter if you initiate on  
the same thread or different thread.

-Alan.


More information about the nio-discuss mailing list