AsynchronousSocketChannel write()

向雅 fyaoxy at gmail.com
Mon Aug 23 11:12:50 PDT 2010


OK good point!
2010/8/23 Avinash Lakshman <avinash.lakshman at gmail.com>:
> 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 my view the ONLY incomplete case should be WRITE, and the reason of
its exist should ONLY be abort case,
through itself still rarely.
> in Windows I/O completion ports. Wouldn't that be a cleaner and trueer
> asynchronous model?

I think about what's wrong with aio framework for some times, as you
known, there no perfect AIO thing  in the world:)
it's why so not a few framework presents, just like c++ world string class.
OK, to the NIO.2, in my view, the focus is the channel operation
Future<>, it's parameter type is too simple,
and it lead the callback interface too simple.
Look, almost all the callback must handle with the channel and buffer.
in another word, we must almost have a place to contains the channel
and the operation buffer.
there are 2 kind of use case by the callback: statefull and stateless.
the only 3 case to use in the callback:
1, wrap channel the current buffer in the evil attachment parameter:)
so called connection.
2, pass the buffer as attachment, the callback must be statefull,
which must hold the channel container
3,put the channel and operation buffer to callback class, higher level
protocol as the attachment
Please focus the 3rd, NIO.2 internal operation task hold right the
channel and the buffer. I think, maybe it can be exposed.

Alan, you have will to explain to all us some design considerations?
Many thanks:)
BTW the asSocketChannel not have a connect timeout, miss or some other
considerations?

>
> 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.
>
>


致敬
向雅


More information about the nio-discuss mailing list