AsynchronousChannel.close()

4 messages Options
Embed this post
Permalink
Gili

AsynchronousChannel.close()

Reply Threaded More More options
Print post
Permalink
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.

Thanks,
Gili
Alan Bateman

Re: AsynchronousChannel.close()

Reply Threaded More More options
Print post
Permalink
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.
Gili

Re: AsynchronousChannel.close()

Reply Threaded More More options
Print post
Permalink
Okay, this is what I thought at first. What threw me off is... how can multiple threads be waiting on the result of the same I/O operation?

Thanks,
Gili

Alan Bateman wrote:
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.
Alan Bateman

Re: AsynchronousChannel.close()

Reply Threaded More More options
Print post
Permalink
Gili wrote:
> Okay, this is what I thought at first. What threw me off is... how can
> multiple threads be waiting on the result of the same I/O operation?
>  
It would be unusual, but in theory you could have several threads
blocked on the Future's get method waiting for the result. If you call
the cancel method then they will all throw CancellationException.

-Alan.