AsynchronousSocketChannel write()
Avinash Lakshman
avinash.lakshman at gmail.com
Mon Aug 23 08:02:33 PDT 2010
Got it. However please correct me if I am wrong. Can't the JDK level API
implementation take care of handling incomplete writes/reads and call the
completion handler only on true I/O completion. That is the way things work
in Windows I/O completion ports. Wouldn't that be a cleaner and trueer
asynchronous model?
Cheers
Avinash
On Thu, Aug 19, 2010 at 1:19 AM, Alan Bateman <Alan.Bateman at oracle.com>wrote:
> Avinash Lakshman wrote:
>
>> Hi All
>>
>> I want to use the write() on the AsynchronousSocketChannel which has no
>> timeout semantics. I also want to pool these connections that I use. Now if
>> I have multiple threads writing into the same connection, even though the
>> writes are synchronized, I get a WritePendingException because a previous
>> write has not completed. What is the general paradigm for handling this? Do
>> I need to keep track of pending writes etc?
>>
> I wonder if this is the right API for what you are doing. If you've got
> multiple threads wanting to write to the same stream then the writing will
> need to be coordinated to avoid "corrupting" the stream. A write to a
> channel is not guaranteed to write all the remaining bytes in the buffer and
> so the buffer prepared by one of the threads may actually require several
> I/O operations. The only thing I can suggest is that you maintain a queue of
> buffers to be written, and use the the completion notification to initiate
> the next write operation (which might be from the same buffer, or the next
> buffer in the queue). I think there are other folks on this mailing list
> that have been through this before and they might be able to advise or
> provide samples from their own usage.
>
> -Alan.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-discuss/attachments/20100823/d1fa0e5d/attachment.html
More information about the nio-discuss
mailing list