An asynchronous channel for stream-oriented listening sockets.html
An asynchronous server-socket channel is created by invoking the open
method of this class. A newly-created asynchronous server-socket channel is open but not yet bound. It can be bound to a local address and configured to listen for connections by invoking the bind
method. Once bound, the accept
method is used to initiate the accepting of connections to the channel's socket. An attempt to invoke the accept method on an unbound channel will cause aNotYetBoundException
to be thrown.java
Channels of this type are safe for use by multiple concurrent threads though at most one accept operation can be outstanding at any time. If a thread initiates an accept operation before a previous accept operation has completed then an AcceptPendingException
will be thrown.api
Socket options are configured using the setOption
method. Channels of this type support the following options:oracle
Usage Example:socket
final AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(5000)); listener.accept(null, new CompletionHandler<AsynchronousSocketChannel,Void>() { public void completed(AsynchronousSocketChannel ch, Void att) { // accept the next connection listener.accept(null, this); // handle this connection handle(ch); } public void failed(Throwable exc, Void att) { ... } });
Asynchronous socket channels are created in one of two ways. A newly-created AsynchronousSocketChannel
is created by invoking one of the open
methods defined by this class. A newly-created channel is open but not yet connected. A connected AsynchronousSocketChannel
is created when a connection is made to the socket of an AsynchronousServerSocketChannel
. It is not possible to create an asynchronous socket channel for an arbitrary, pre-existingsocket
.async
A channel that supports asynchronous I/O operations. Asynchronous I/O operations will usually take one of two forms:ide
Future<V> operation(...)
void operation(... A attachment, CompletionHandler<V,? super A> handler)
In the first form, the methods defined by the Future
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 CompletionHandler
is invoked to consume the result of the I/O operation when it completes or fails.ui
A grouping of asynchronous channels for the purpose of resource sharing.this
An asynchronous channel group encapsulates the mechanics required to handle the completion of I/O operations initiated by asynchronous channels
that are bound to the group. A group has an associated thread pool to which tasks are submitted to handle I/O events and dispatch to completion-handlers
that consume the result of asynchronous operations performed on channels in the group. In addition to handling I/O events, the pooled threads may also execute other tasks required to support the execution of asynchronous I/O operations.spa
An asynchronous channel group is created by invoking the withFixedThreadPool
or withCachedThreadPool
methods defined here. Channels are bound to a group by specifying the group when constructing the channel. The associated thread pool is owned by the group; termination of the group results in the shutdown of the associated thread pool.
In addition to groups created explicitly, the Java virtual machine maintains a system-wide default group that is constructed automatically. Asynchronous channels that do not specify a group at construction time are bound to the default group. The default group has an associated thread pool that creates new threads as needed. The default group may be configured by means of system properties defined in the table below. Where the ThreadFactory
for the default group is not configured then the pooled threads of the default group are daemon
threads.