Name

InterruptibleChannel

Synopsis

Channels that implement this marker interface have two important properties that are relevant to multithreaded programs: they are asynchonously closeable and interruptible. When the close( ) method of an InterruptibleChannel is called, any other thread that is blocked waiting for an I/O operation to complete on that channel will stop blocking and receive an AsynchronousCloseException. Furthermore, if a thread is blocked waiting for an I/O operation to complete on an InterruptibleChannel, then another thread may call the interrupt( ) method of the blocked thread. This causes the interrupt status of the blocked thread to be set and causes the thread to wake up and receive an ClosedByInterruptException (a subclass of AsynchronousCloseException). As the name of this interrupt implies, the channel that the thread was blocked on is closed as a side-effect of the thread interruption. There is no way to interrupt a blocked thread without closing the channel upon which it is blocked. This ability to interrupt a blocked thread is particularly noteworthy because it has never worked reliably with the older java.io API.

All the concrete channel implementations that are part of this package implement InterruptibleChannel. Note, however, that methods such as Channels.newChannel( ) may return channel objects that are not interruptible. You can use the instanceof to determine whether an unknown channel object implements this interface.

Figure 13-28. java.nio.channels.InterruptibleChannel ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.