Closeable
, AutoCloseable
, Channel
, InterruptibleChannel
FileChannel
, SelectableChannel
public abstract class AbstractInterruptibleChannel extends Object implements Channel, InterruptibleChannel
This class encapsulates the low-level machinery required to implement the asynchronous closing and interruption of channels. A concrete channel class must invoke the begin
and end
methods before and after, respectively, invoking an I/O operation that might block indefinitely. In order to ensure that the end
method is always invoked, these methods should be used within a try
... finally
block:
boolean completed = false;
try {
begin();
completed = ...; // Perform blocking I/O operation
return ...; // Return result
} finally {
end(completed);
}
The completed
argument to the end
method tells whether or not the I/O operation actually completed, that is, whether it had any effect that would be visible to the invoker. In the case of an operation that reads bytes, for example, this argument should be true
if, and only if, some bytes were actually transferred into the invoker's target buffer.
A concrete channel class must also implement the implCloseChannel
method in such a way that if it is invoked while another thread is blocked in a native I/O operation upon the channel then that operation will immediately return, either by throwing an exception or by returning normally. If a thread is interrupted or the channel upon which it is blocked is asynchronously closed then the channel's end
method will throw the appropriate exception.
This class performs the synchronization required to implement the Channel
specification. Implementations of the implCloseChannel
method need not synchronize against other threads that might be attempting to close the channel.
Modifier | Constructor | Description |
---|---|---|
protected |
Initializes a new instance of this class. |
Modifier and Type | Method | Description |
---|---|---|
protected final void |
begin() |
Marks the beginning of an I/O operation that might block indefinitely. |
final void |
close() |
Closes this channel. |
protected final void |
end |
Marks the end of an I/O operation that might block indefinitely. |
protected abstract void |
implCloseChannel() |
Closes this channel. |
final boolean |
isOpen() |
Tells whether or not this channel is open. |
protected AbstractInterruptibleChannel()
public final void close() throws IOException
If the channel has already been closed then this method returns immediately. Otherwise it marks the channel as closed and then invokes the implCloseChannel
method in order to complete the close operation.
close
in interface AutoCloseable
close
in interface Channel
close
in interface Closeable
close
in interface InterruptibleChannel
IOException
- If an I/O error occursprotected abstract void implCloseChannel() throws IOException
This method is invoked by the close
method in order to perform the actual work of closing the channel. This method is only invoked if the channel has not yet been closed, and it is never invoked more than once.
An implementation of this method must arrange for any other thread that is blocked in an I/O operation upon this channel to return immediately, either by throwing an exception or by returning normally.
IOException
- If an I/O error occurs while closing the channelpublic final boolean isOpen()
Channel
protected final void begin()
protected final void end(boolean completed) throws AsynchronousCloseException
This method should be invoked in tandem with the begin
method, using a try
... finally
block as shown above, in order to implement asynchronous closing and interruption for this channel.
completed
- true
if, and only if, the I/O operation completed successfully, that is, had some effect that would be visible to the operation's invokerAsynchronousCloseException
- If the channel was asynchronously closedClosedByInterruptException
- If the thread blocked in the I/O operation was interrupted
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/channels/spi/AbstractInterruptibleChannel.html