Class AbstractInterruptibleChannel
- All Implemented Interfaces:
Closeable, AutoCloseable, Channel, InterruptibleChannel
- Direct Known Subclasses:
FileChannel, SelectableChannel
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.
- Since:
- 1.4
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInitializes a new instance of this class. -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidbegin()Marks the beginning of an I/O operation that might block indefinitely.final voidclose()Closes this channel.protected final voidend(boolean completed) Marks the end of an I/O operation that might block indefinitely.protected abstract voidCloses this channel.final booleanisOpen()Tells whether or not this channel is open.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Constructor Details
-
AbstractInterruptibleChannel
protected AbstractInterruptibleChannel()Initializes a new instance of this class.
-
-
Method Details
-
close
Closes this channel.If the channel has already been closed then this method returns immediately. Otherwise it marks the channel as closed and then invokes the
implCloseChannelmethod in order to complete the close operation.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceInterruptibleChannel- Throws:
IOException- If an I/O error occurs
-
implCloseChannel
Closes this channel.This method is invoked by the
closemethod 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.
- Throws:
IOException- If an I/O error occurs while closing the channel
-
isOpen
-
begin
-
end
Marks the end of an I/O operation that might block indefinitely.This method should be invoked in tandem with the
beginmethod, using atry...finallyblock as shown above, in order to implement asynchronous closing and interruption for this channel.- Parameters:
completed-trueif, and only if, the I/O operation completed successfully, that is, had some effect that would be visible to the operation's invoker- Throws:
AsynchronousCloseException- If the channel was asynchronously closedClosedByInterruptException- If the thread blocked in the I/O operation was interrupted
-