An asynchronous channel group encapsulates the mechanics required to handle the completion of I/O operations initiated by {@link AsynchronousChannel asynchronous channels} that are bound to the group. A group has an associatedthread pool to which tasks are submitted to handle I/O events and dispatch to {@link CompletionHandler completion-handlers} that consume the result ofasynchronous 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.
An asynchronous channel group is created by invoking the {@link #withFixedThreadPool withFixedThreadPool} or {@link #withCachedThreadPool withCachedThreadPool} methods defined here. Channels are bound to a group byspecifying 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 {@link java.util.concurrent.ThreadFactory ThreadFactory} for thedefault group is not configured then the pooled threads of the default group are {@link Thread#isDaemon daemon} threads.
System property | Description |
---|---|
{@code java.nio.channels.DefaultThreadPool.threadFactory} | The value of this property is taken to be the fully-qualified name of a concrete {@link java.util.concurrent.ThreadFactory ThreadFactory}class. The class is loaded using the system class loader and instantiated. The factory's {@link java.util.concurrent.ThreadFactory#newThread newThread} method is invoked to create each thread for the defaultgroup's thread pool. If the process to load and instantiate the value of the property fails then an unspecified error is thrown during the construction of the default group. |
{@code java.nio.channels.DefaultThreadPool.initialSize} | The value of the {@code initialSize} parameter for the defaultgroup (see {@link #withCachedThreadPool withCachedThreadPool}). The value of the property is taken to be the {@code String}representation of an {@code Integer} that is the initial size parameter.If the value cannot be parsed as an {@code Integer} it causes anunspecified error to be thrown during the construction of the default group. |
The completion handler for an I/O operation initiated on a channel bound to a group is guaranteed to be invoked by one of the pooled threads in the group. This ensures that the completion handler is run by a thread with the expected identity.
Where an I/O operation completes immediately, and the initiating thread is one of the pooled threads in the group then the completion handler may be invoked directly by the initiating thread. To avoid stack overflow, an implementation may impose a limit as to the number of activations on the thread stack. Some I/O operations may prohibit invoking the completion handler directly by the initiating thread (see {@link AsynchronousServerSocketChannel#accept(Object,CompletionHandler) accept}).
The {@link #shutdown() shutdown} method is used to initiate an orderlyshutdown of a group. An orderly shutdown marks the group as shutdown; further attempts to construct a channel that binds to the group will throw {@link ShutdownChannelGroupException}. Whether or not a group is shutdown can be tested using the {@link #isShutdown() isShutdown} method. Once shutdown,the group terminates when all asynchronous channels that are bound to the group are closed, all actively executing completion handlers have run to completion, and resources used by the group are released. No attempt is made to stop or interrupt threads that are executing completion handlers. The {@link #isTerminated() isTerminated} method is used to test if the group hasterminated, and the {@link #awaitTermination awaitTermination} method can beused to block until the group has terminated.
The {@link #shutdownNow() shutdownNow} method can be used to initiate aforceful shutdown of the group. In addition to the actions performed by an orderly shutdown, the {@code shutdownNow} method closes all open channelsin the group as if by invoking the {@link AsynchronousChannel#close close}method. @since 1.7 @see AsynchronousSocketChannel#open(AsynchronousChannelGroup) @see AsynchronousServerSocketChannel#open(AsynchronousChannelGroup)
|
|
|
|
|
|
|
|
|
|