hg: nio/nio/jdk: First installation of potential updates for M4
Alexander Libman
libman at terabit.com.au
Thu May 14 20:24:58 PDT 2009
> Future#cancel is hard to get right, as I learned the hard way :-)
Well known, I had same problems with TProactor in C++ :)
>
> The main thing to know is that cancel needs to return true irrespective
> of the value of the mayInterruptIfRunning parameter (assuming the I/O
> operation hasn't completed or hasn't been cancelled of course, in which
> case it will return false). You also need to make sure that subsequent
> calls to isDone return true and any calls to get throw CancelledException.
I am thinking about real use case of cancel() and can not find a good
example..
If cancel() returns true, but leaves channel in unspecified state and all
you can do is
only close the channel.
Assuming that we manage to cancel read for streams/sockets,
then it does not mean that we can receive different data for another read.
Cancel may be usefull only for regular files. But also hard to find an
example.
For the filters, we can cancel operation and leave channel in consistent
state,
only if buffers were not modified. To do this we have to maintain Future
state:
RUNNING, PENDING, COMPLETED, FAILED, CANCELLED
Basically, 3 last states are substates of DONE.
RUNNING means operation is active, i.e. filter works with buffers and
PENDING means that operation is cancellable,i.e. filter currently does not
work with buffers.
State is AtomicInteger .
I think similar mechanizm used in NIO2.
But support of cancel() makes code much more complicated
The quiestion: does it make sense to implement cancellation of particular
operation ?
Currently, I believe that the following implementation will be enough (sorry
for repetition):
cancel (boolean mayInterrupt)
{
if (mayInterrupt) {
channel.close();
}
return false;
}
In other words, cancel is always NOOP if mayInterrupt is false and
otherwise hidden form of close() via Future method.
And this implementation complies to the specification.
May be it does make sense to declare cancel() strictly in such way and
forget about pain with
cancellation of particular operation?
Alex
More information about the nio-dev
mailing list