Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

A fiber executing in cancellable scope may be cancelled by invoking its cancel method to set the fiber’s cancel status and unpark the fiber. Cancellation works cooperatively: a Fiber needs to check for cancellation (say, when doing blocking operations), and throw an appropriate exception for the context that it is running in. Examples of operation operations that check for cancellation are Thread.sleep and blocking socket operations such as connect, accept, read, and write. 

At this time, the blocking operations wakeup and throw an exception when the fiber is cancelled:

  • java.lang.Thread.sleep
  • java.net.Socket: connect, read, write
  • java.net.ServerSocket: accept
  • java.nio.channels.SocketChannel: connect, read, write
  • java.nio.channels.ServerSocketChannel: accept
  • java.nio.channels.DatagramChannel: read, receive
  • java.nio.channels.Pipe.SourceChannel.read
  • java.nio.channels.Pipe.SinkChannel.write

There are rare, but important, cases such as shutdown or recovery steps where a fiber may need to be shielded from cancellation. To support this, FiberScope defines the nonCancellable() method to enter a non-cancellable scope. A Fiber that checks for cancellation in a non-cancellation scope will get back “false”. If its cancel status is set then it will observe this when it pops back to a cancellable scope.

...