AsynchronousSocketChannel write()
Alan Bateman
Alan.Bateman at oracle.com
Thu Aug 19 01:19:29 PDT 2010
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.
More information about the nio-discuss
mailing list