FileDocCategorySizeDatePackage
AbstractInterruptibleChannel.javaAPI DocAndroid 1.5 API7281Wed May 06 22:41:04 BST 2009java.nio.channels.spi

AbstractInterruptibleChannel

public abstract class AbstractInterruptibleChannel extends Object implements InterruptibleChannel, Channel
{@code AbstractInterruptibleChannel} is the root class for interruptible channels.

The basic usage pattern for an interruptible channel is to invoke {@code begin()} before any I/O operation that potentially blocks indefinitely, then {@code end(boolean)} after completing the operation. The argument to the {@code end} method should indicate if the I/O operation has actually completed so that any change may be visible to the invoker.

since
Android 1.0

Fields Summary
static Method
setInterruptAction
private volatile boolean
closed
volatile boolean
interrupted
Constructors Summary
protected AbstractInterruptibleChannel()
Default constructor.

since
Android 1.0


               
      
        super();
    
Methods Summary
protected final voidbegin()
Indicates the beginning of a code section that includes an I/O operation that is potentially blocking. After this operation, the application should invoke the corresponding {@code end(boolean)} method.

since
Android 1.0

        // FIXME: be accommodate before VM actually provides
        // setInterruptAction method
        if (setInterruptAction != null) {
            try {
                setInterruptAction.invoke(Thread.currentThread(),
                        new Object[] { new Runnable() {
                            public void run() {
                                try {
                                    interrupted = true;
                                    AbstractInterruptibleChannel.this.close();
                                } catch (IOException e) {
                                    // ignore
                                }
                            }
                        } });
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
public final voidclose()
Closes an open channel. If the channel is already closed then this method has no effect, otherwise it closes the receiver via the {@code implCloseChannel} method.

If an attempt is made to perform an operation on a closed channel then a {@link java.nio.channels.ClosedChannelException} is thrown.

If multiple threads attempt to simultaneously close a channel, then only one thread will run the closure code and the others will be blocked until the first one completes.

throws
IOException if a problem occurs while closing this channel.
since
Android 1.0

        if (!closed) {
            synchronized (this) {
                if (!closed) {
                    closed = true;
                    implCloseChannel();
                }
            }
        }
    
protected final voidend(boolean success)
Indicates the end of a code section that has been started with {@code begin()} and that includes a potentially blocking I/O operation.

param
success pass {@code true} if the blocking operation has succeeded and has had a noticeable effect; {@code false} otherwise.
throws
AsynchronousCloseException if this channel is closed by another thread while this method is executing.
throws
ClosedByInterruptException if another thread interrupts the calling thread while this method is executing.
since
Android 1.0

        // FIXME: be accommodate before VM actually provides
        // setInterruptAction method
        if (setInterruptAction != null) {
            try {
                setInterruptAction.invoke(Thread.currentThread(),
                        new Object[] { null });
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            if (interrupted) {
                interrupted = false;
                throw new ClosedByInterruptException();
            }
        }
        if (!success && closed) {
            throw new AsynchronousCloseException();
        }
    
protected abstract voidimplCloseChannel()
Implements the channel closing behavior.

Closes the channel with a guarantee that the channel is not currently closed through another invocation of {@code close()} and that the method is thread-safe.

Any outstanding threads blocked on I/O operations on this channel must be released with either a normal return code, or by throwing an {@code AsynchronousCloseException}.

throws
IOException if a problem occurs while closing the channel.
since
Android 1.0

public final synchronized booleanisOpen()
Indicates whether this channel is open.

return
{@code true} if this channel is open, {@code false} if it is closed.
see
java.nio.channels.Channel#isOpen()
since
Android 1.0

        return !closed;