# AsynchronousChannel

public interface **AsynchronousChannel** implements [Channel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channel.html)

|---|---|---|
| Known Indirect Subclasses [AsynchronousByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousByteChannel.html), [AsynchronousFileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html), [AsynchronousServerSocketChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousServerSocketChannel.html), [AsynchronousSocketChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousSocketChannel.html) |---|---| | [AsynchronousByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousByteChannel.html) | An asynchronous channel that can read and write bytes. | | [AsynchronousFileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html) | An asynchronous channel for reading, writing, and manipulating a file. | | [AsynchronousServerSocketChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousServerSocketChannel.html) | An asynchronous channel for stream-oriented listening sockets. | | [AsynchronousSocketChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousSocketChannel.html) | An asynchronous channel for stream-oriented connecting sockets. | |||

A channel that supports asynchronous I/O operations. Asynchronous I/O
operations will usually take one of two forms:

1.

       https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html<V> operation(...)

2.

       void operation(... A attachment, https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CompletionHandler.html<V,? super A> handler)

where *operation* is the name of the I/O operation (read or write for example), *V* is the result type of the I/O operation, and *A* is the type of an object attached to the I/O operation to provide context when consuming the result. The attachment is important for cases where a *state-less* `CompletionHandler` is used to consume the result of many I/O operations.

In the first form, the methods defined by the `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html`
interface may be used to check if the operation has completed, wait for its
completion, and to retrieve the result. In the second form, a `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CompletionHandler.html` is invoked to consume the result of the I/O operation when
it completes or fails.

A channel that implements this interface is *asynchronously
closeable* : If an I/O operation is outstanding on the channel and the
channel's `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousChannel.html#close()` method is invoked, then the I/O operation
fails with the exception `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousCloseException.html`.

Asynchronous channels are safe for use by multiple concurrent threads.
Some channel implementations may support concurrent reading and writing, but
may not allow more than one read and one write operation to be outstanding at
any given time.

## Cancellation

The `Future` interface defines the `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#cancel(boolean)`
method to cancel execution. This causes all threads waiting on the result of
the I/O operation to throw `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CancellationException.html`.
Whether the underlying I/O operation can be cancelled is highly implementation
specific and therefore not specified. Where cancellation leaves the channel,
or the entity to which it is connected, in an inconsistent state, then the
channel is put into an implementation specific *error state* that
prevents further attempts to initiate I/O operations that are *similar*
to the operation that was cancelled. For example, if a read operation is
cancelled but the implementation cannot guarantee that bytes have not been
read from the channel then it puts the channel into an error state; further
attempts to initiate a `read` operation cause an unspecified runtime
exception to be thrown. Similarly, if a write operation is cancelled but the
implementation cannot guarantee that bytes have not been written to the
channel then subsequent attempts to initiate a `write` will fail with
an unspecified runtime exception.

Where the `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#cancel(boolean)` method is invoked with the `mayInterruptIfRunning` parameter set to `true` then the I/O operation
may be interrupted by closing the channel. In that case all threads waiting
on the result of the I/O operation throw `CancellationException` and
any other I/O operations outstanding on the channel complete with the
exception `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousCloseException.html`.

Where the `cancel` method is invoked to cancel read or write
operations then it is recommended that all buffers used in the I/O operations
be discarded or care taken to ensure that the buffers are not accessed while
the channel remains open.

### Public Method Summary

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousChannel.html#close())() Closes this channel. |

### Inherited Method Summary

From interface [java.nio.channels.Channel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channel.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channel.html#close())() Closes this channel. |
| abstract boolean | [isOpen](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channel.html#isOpen())() Tells whether or not this channel is open. |

From interface [java.io.Closeable](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html#close())() Closes this stream and releases any system resources associated with it. |

From interface [java.lang.AutoCloseable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html#close())() Closes this resource, relinquishing any underlying resources. |

## Public Methods

#### public abstract void
**close**
()

Closes this channel.

Any outstanding asynchronous operations upon this channel will
complete with the exception `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousCloseException.html`. After a
channel is closed, further attempts to initiate asynchronous I/O
operations complete immediately with cause `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ClosedChannelException.html`.

This method otherwise behaves exactly as specified by the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channel.html` interface.

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If an I/O error occurs |
|---|---|