Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Protected Ctors](https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#proctors) \| [Methods](https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#inhmethods)

# AsynchronousChannelGroup


`
public


abstract
class
AsynchronousChannelGroup
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.nio.channels.AsynchronousChannelGroup |

<br />

*** ** * ** ***

A grouping of asynchronous channels for the purpose of resource sharing.

An asynchronous channel group encapsulates the mechanics required to
handle the completion of I/O operations initiated by `https://developer.android.com/reference/java/nio/channels/AsynchronousChannel` 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
`https://developer.android.com/reference/java/nio/channels/CompletionHandler` 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.

An asynchronous channel group is created by invoking the `https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#withFixedThreadPool(int,%20java.util.concurrent.ThreadFactory)` or `https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#withCachedThreadPool(java.util.concurrent.ExecutorService,%20int)` 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 `https://developer.android.com/reference/java/util/concurrent/ThreadFactory` for the
default group is not configured then the pooled threads of the default group
are `https://developer.android.com/reference/java/lang/Thread#isDaemon()` threads.

| System property | Description |
| `java.nio.channels.DefaultThreadPool.threadFactory` | The value of this property is taken to be the fully-qualified name of a concrete `https://developer.android.com/reference/java/util/concurrent/ThreadFactory` class. The class is loaded using the system class loader and instantiated. The factory's `https://developer.android.com/reference/java/util/concurrent/ThreadFactory#newThread(java.lang.Runnable)` method is invoked to create each thread for the default group'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. |
| `java.nio.channels.DefaultThreadPool.initialSize` | The value of the `initialSize` parameter for the default group (see `https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#withCachedThreadPool(java.util.concurrent.ExecutorService,%20int)`). The value of the property is taken to be the `String` representation of an `Integer` that is the initial size parameter. If the value cannot be parsed as an `Integer` it causes an unspecified error to be thrown during the construction of the default group. |
|---|---|
[System properties]

## Threading

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 `https://developer.android.com/reference/java/nio/channels/AsynchronousServerSocketChannel#accept(A,%20java.nio.channels.CompletionHandler<java.nio.channels.AsynchronousSocketChannel,?%20super%20A>)`).

## Shutdown and Termination

The `https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#shutdown()` method is used to initiate an *orderly
shutdown* of a group. An orderly shutdown marks the group as shutdown;
further attempts to construct a channel that binds to the group will throw
`https://developer.android.com/reference/java/nio/channels/ShutdownChannelGroupException`. Whether or not a group is shutdown can
be tested using the `https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#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
`https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#isTerminated()` method is used to test if the group has
terminated, and the `https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#awaitTermination(long,%20java.util.concurrent.TimeUnit)` method can be
used to block until the group has terminated.

The `https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#shutdownNow()` method can be used to initiate a
*forceful shutdown* of the group. In addition to the actions performed
by an orderly shutdown, the `shutdownNow` method closes all open channels
in the group as if by invoking the `https://developer.android.com/reference/java/nio/channels/AsynchronousChannel#close()`
method.
**See also:**

- `https://developer.android.com/reference/java/nio/channels/AsynchronousSocketChannel#open(java.nio.channels.AsynchronousChannelGroup)`
- `https://developer.android.com/reference/java/nio/channels/AsynchronousServerSocketChannel#open(java.nio.channels.AsynchronousChannelGroup)`

## Summary

| ### Protected constructors ||
|---|---|
| ` https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#AsynchronousChannelGroup(java.nio.channels.spi.AsynchronousChannelProvider)(https://developer.android.com/reference/java/nio/channels/spi/AsynchronousChannelProvider provider) ` Initialize a new instance of this class. |

| ### Public methods ||
|---|---|
| ` abstract boolean` | ` https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#awaitTermination(long,%20java.util.concurrent.TimeUnit)(long timeout, https://developer.android.com/reference/java/util/concurrent/TimeUnit unit) ` Awaits termination of the group. |
| ` abstract boolean` | ` https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#isShutdown()() ` Tells whether or not this asynchronous channel group is shutdown. |
| ` abstract boolean` | ` https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#isTerminated()() ` Tells whether or not this group has terminated. |
| ` final https://developer.android.com/reference/java/nio/channels/spi/AsynchronousChannelProvider` | ` https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#provider()() ` Returns the provider that created this channel group. |
| ` abstract void` | ` https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#shutdown()() ` Initiates an orderly shutdown of the group. |
| ` abstract void` | ` https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#shutdownNow()() ` Shuts down the group and closes all open channels in the group. |
| ` static https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup` | ` https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#withCachedThreadPool(java.util.concurrent.ExecutorService,%20int)(https://developer.android.com/reference/java/util/concurrent/ExecutorService executor, int initialSize) ` Creates an asynchronous channel group with a given thread pool that creates new threads as needed. |
| ` static https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup` | ` https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#withFixedThreadPool(int,%20java.util.concurrent.ThreadFactory)(int nThreads, https://developer.android.com/reference/java/util/concurrent/ThreadFactory threadFactory) ` Creates an asynchronous channel group with a fixed thread pool. |
| ` static https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup` | ` https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#withThreadPool(java.util.concurrent.ExecutorService)(https://developer.android.com/reference/java/util/concurrent/ExecutorService executor) ` Creates an asynchronous channel group with a given thread pool. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Protected constructors

### AsynchronousChannelGroup

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected AsynchronousChannelGroup (AsynchronousChannelProvider provider)
```

Initialize a new instance of this class.

<br />

| Parameters ||
|---|---|
| `provider` | `AsynchronousChannelProvider`: The asynchronous channel provider for this group <br /> |

## Public methods

### awaitTermination

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract boolean awaitTermination (long timeout, 
                TimeUnit unit)
```

Awaits termination of the group.

This method blocks until the group has terminated, or the timeout
occurs, or the current thread is interrupted, whichever happens first.

<br />

| Parameters ||
|---|---|
| `timeout` | `long`: The maximum time to wait, or zero or less to not wait <br /> |
| `unit` | `TimeUnit`: The time unit of the timeout argument <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if the group has terminated; `false` if the timeout elapsed before termination <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/InterruptedException` | If interrupted while waiting |

### isShutdown

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract boolean isShutdown ()
```

Tells whether or not this asynchronous channel group is shutdown.

<br />

| Returns ||
|---|---|
| `boolean` | `true` if this asynchronous channel group is shutdown or has been marked for shutdown. <br /> |

### isTerminated

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract boolean isTerminated ()
```

Tells whether or not this group has terminated.

Where this method returns `true`, then the associated thread
pool has also `https://developer.android.com/reference/java/util/concurrent/ExecutorService#isTerminated()`.

<br />

| Returns ||
|---|---|
| `boolean` | `true` if this group has terminated <br /> |

### provider

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final AsynchronousChannelProvider provider ()
```

Returns the provider that created this channel group.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/spi/AsynchronousChannelProvider` | The provider that created this channel group <br /> |

### shutdown

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract void shutdown ()
```

Initiates an orderly shutdown of the group.

This method marks the group as shutdown. Further attempts to construct
channel that binds to this group will throw `https://developer.android.com/reference/java/nio/channels/ShutdownChannelGroupException`.
The group terminates when all asynchronous channels in the group are
closed, all actively executing completion handlers have run to completion,
and all resources have been released. This method has no effect if the
group is already shutdown.

<br />

### shutdownNow

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract void shutdownNow ()
```

Shuts down the group and closes all open channels in the group.

In addition to the actions performed by the `https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup#shutdown()`
method, this method invokes the `https://developer.android.com/reference/java/nio/channels/AsynchronousChannel#close()`
method on all open channels in the group. This method does not attempt to
stop or interrupt threads that are executing completion handlers. The
group terminates when all actively executing completion handlers have run
to completion and all resources have been released. This method may be
invoked at any time. If some other thread has already invoked it, then
another invocation will block until the first invocation is complete,
after which it will return without effect.

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If an I/O error occurs |

### withCachedThreadPool

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static AsynchronousChannelGroup withCachedThreadPool (ExecutorService executor, 
                int initialSize)
```

Creates an asynchronous channel group with a given thread pool that
creates new threads as needed.

The `executor` parameter is an `ExecutorService` that
creates new threads as needed to execute tasks that are submitted to
handle I/O events and dispatch completion results for operations initiated
on asynchronous channels in the group. It may reuse previously constructed
threads when they are available.

The `initialSize` parameter may be used by the implementation
as a *hint* as to the initial number of tasks it may submit. For
example, it may be used to indicate the initial number of threads that
wait on I/O events.

The executor is intended to be used exclusively by the resulting
asynchronous channel group. Termination of the group results in the
orderly `https://developer.android.com/reference/java/util/concurrent/ExecutorService#shutdown()` of the executor
service. Shutting down the executor service by other means results in
unspecified behavior.

The group is created by invoking the `https://developer.android.com/reference/java/nio/channels/spi/AsynchronousChannelProvider#openAsynchronousChannelGroup(java.util.concurrent.ExecutorService,%20int)` method of the system-wide
default `https://developer.android.com/reference/java/nio/channels/spi/AsynchronousChannelProvider` object.

<br />

| Parameters ||
|---|---|
| `executor` | `ExecutorService`: The thread pool for the resulting group <br /> |
| `initialSize` | `int`: A value `>=0` or a negative value for implementation specific default <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup` | A new asynchronous channel group <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If an I/O error occurs |

**See also:**

- `https://developer.android.com/reference/java/util/concurrent/Executors#newCachedThreadPool()`

### withFixedThreadPool

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static AsynchronousChannelGroup withFixedThreadPool (int nThreads, 
                ThreadFactory threadFactory)
```

Creates an asynchronous channel group with a fixed thread pool.

The resulting asynchronous channel group reuses a fixed number of
threads. At any point, at most `nThreads` threads will be active
processing tasks that are submitted to handle I/O events and dispatch
completion results for operations initiated on asynchronous channels in
the group.

The group is created by invoking the `https://developer.android.com/reference/java/nio/channels/spi/AsynchronousChannelProvider#openAsynchronousChannelGroup(int,%20java.util.concurrent.ThreadFactory)` method of the system-wide
default `https://developer.android.com/reference/java/nio/channels/spi/AsynchronousChannelProvider` object.

<br />

| Parameters ||
|---|---|
| `nThreads` | `int`: The number of threads in the pool <br /> |
| `threadFactory` | `ThreadFactory`: The factory to use when creating new threads <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup` | A new asynchronous channel group <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If an I/O error occurs |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | If `nThreads <= 0` |

### withThreadPool

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static AsynchronousChannelGroup withThreadPool (ExecutorService executor)
```

Creates an asynchronous channel group with a given thread pool.

The `executor` parameter is an `ExecutorService` that
executes tasks submitted to dispatch completion results for operations
initiated on asynchronous channels in the group.

Care should be taken when configuring the executor service. It
should support *direct handoff* or *unbounded queuing* of
submitted tasks, and the thread that invokes the `https://developer.android.com/reference/java/util/concurrent/Executor#execute(java.lang.Runnable)` method should never invoke the task
directly. An implementation may mandate additional constraints.

The executor is intended to be used exclusively by the resulting
asynchronous channel group. Termination of the group results in the
orderly `https://developer.android.com/reference/java/util/concurrent/ExecutorService#shutdown()` of the executor
service. Shutting down the executor service by other means results in
unspecified behavior.

The group is created by invoking the `https://developer.android.com/reference/java/nio/channels/spi/AsynchronousChannelProvider#openAsynchronousChannelGroup(java.util.concurrent.ExecutorService,%20int)` method of the system-wide
default `https://developer.android.com/reference/java/nio/channels/spi/AsynchronousChannelProvider` object with an `initialSize` of `0`.

<br />

| Parameters ||
|---|---|
| `executor` | `ExecutorService`: The thread pool for the resulting group <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/AsynchronousChannelGroup` | A new asynchronous channel group <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If an I/O error occurs |