AsynchronousChannel.close()

Alan Bateman Alan.Bateman at Sun.COM
Thu Jun 25 00:48:52 PDT 2009


Gili wrote:
> Hi,
>
> The Javadoc for AsynchronousChannel reads "Where the cancel method is
> invoked with the mayInterruptIfRunning parameter set to true then the I/O
> operation may be interrupted by closing the channel. In that case all
> threads waiting on the result of the I/O operation throw
> CancellationException and any other I/O operations outstanding on the
> channel complete with the exception AsynchronousCloseException."
>
> I don't understand the last part. Are you implying that all Futures get
> CancellationException whereas all CompletionHandlers get
> AsynchronousCloseException? Or did you mean something else?
>
> The paragraph makes it seem as if Futures that are not actively being waited
> on will throw AsynchronousCloseException when get() is eventually invoked.
> This sounds rather difficult from an implementation point of view. Please
> clarify.
>   
The spec allows a forceful cancel (mayInterruptIfRunning parameter set 
to true) to be implemented as an asynchronous close. This will cause 
"other" I/O operations outstanding on the channel to fail with 
AsynchronousCloseException. So for example, suppose you forcefully 
cancel a write while there is read outstanding. All threads waiting on 
the result of the write will throw CancellationException. Assuming the 
implementation closes the channel, then it will cause the read to fail 
with an AsynchronousCloseException. If the read was initiated specifying 
a completion handler, then the completion handler's failed method will 
be invoked with the exception (AsynchronousCloseException). 
Alternatively, if the read was initiated returning a Future then any 
threads waiting on the read result will throw ExecutionException with 
AsynchronousCloseException as the cause. Does that help?

-Alan.



More information about the nio-discuss mailing list