public abstract class AsynchronousChannelGroup extends Object
异步通道组封装需要处理完成I/O操作由asynchronous channels
被绑定到组力学。一组有关联的线程池的任务被提交到处理I/O事件调度,消耗的异步操作在集团的渠道进行结果completion-handlers
。除了处理I / O事件,汇集的线程也可以执行其他任务,以支持异步I / O操作的执行。
异步通道组是由调用withFixedThreadPool
或withCachedThreadPool
方法定义。通过指定组在构建信道时,绑定到一个组。关联的线程池由组拥有;组的终止导致关联的线程池的关闭。
除了组显式创建,java虚拟机维护一个系统范围的默认组,自动构造。不指定在施工时间组的异步信道绑定到默认组。默认组有一个关联的线程池,根据需要创建新的线程。默认组可以通过在下面的表中定义的系统属性来配置。在默认的组ThreadFactory
配置不然后合并线程的默认组是daemon
螺纹。
System property | 描述 |
---|---|
java.nio.channels.DefaultThreadPool.threadFactory |
The value of this property is taken to be the fully-qualified name of a concrete ThreadFactory class. The class is loaded using the system class loader and instantiated. The factory's newThread 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 withCachedThreadPool ). 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. |
在绑定到一个组的通道上启动的I / O操作的完成处理程序保证将被组中的汇集线程调用。这将确保完成处理程序是由一个线程与预期的身份运行的。
当一个I / O操作立即完成时,启动线程是该组中的汇集线程之一,然后完成处理程序可以直接调用启动线程。为了避免栈溢出,实现可能限制在线程堆栈的激活数。一些I/O操作可以禁止调用完成处理程序直接由主线程(见accept
)。
的shutdown
方法用于启动一组有序的关机。一个有序的关闭标志着集团为关机;进一步尝试构建了一个渠道,结合集团将把ShutdownChannelGroupException
。是否一组关机可以使用isShutdown
法测试。一旦关闭,该组终止时,所有的异步信道绑定到组关闭,所有积极执行完成处理已运行完成,所使用的资源组被释放。没有尝试停止或中断正在执行完成处理程序的线程。的isTerminated
法测试如果本集团已终止,而awaitTermination
方法可以用来阻止直到集团已终止。
的shutdownNow
方法可以用来发起有力的组关闭。此外,通过有序的关机动作,这shutdownNow
方法关闭该组中的所有公开渠道如通过调用close
方法。
Modifier | Constructor and Description |
---|---|
protected |
AsynchronousChannelGroup(AsynchronousChannelProvider provider)
初始化这个类的一个新实例。
|
Modifier and Type | Method and Description |
---|---|
abstract boolean |
awaitTermination(long timeout, TimeUnit unit)
等待本集团终止。
|
abstract boolean |
isShutdown()
告诉是否这个异步通道组正在关闭。
|
abstract boolean |
isTerminated()
告诉是否该组已终止。
|
AsynchronousChannelProvider |
provider()
返回创建此通道组的提供程序。
|
abstract void |
shutdown()
启动本集团的有序停工。
|
abstract void |
shutdownNow()
关闭组,并关闭组中的所有打开的通道。
|
static AsynchronousChannelGroup |
withCachedThreadPool(ExecutorService executor, int initialSize)
创建一个异步信道组,一个给定的线程池,创建新的线程,需要。
|
static AsynchronousChannelGroup |
withFixedThreadPool(int nThreads, ThreadFactory threadFactory)
创建一个固定的线程池的异步信道组。
|
static AsynchronousChannelGroup |
withThreadPool(ExecutorService executor)
创建一个给定线程池的异步信道组。
|
protected AsynchronousChannelGroup(AsynchronousChannelProvider provider)
provider
-这个组的异步信道提供商
public final AsynchronousChannelProvider provider()
public static AsynchronousChannelGroup withFixedThreadPool(int nThreads, ThreadFactory threadFactory) throws IOException
由此产生的异步信道复用一个固定数量的线程组。在任何时候,在最nThreads
线程将积极处理任务提交处理I/O事件和集团对异步通道开始运营调度的完成结果。
该集团是由调用openAsynchronousChannelGroup(int,ThreadFactory)
方法的默认AsynchronousChannelProvider
对象。
nThreads
-池中的线程数
threadFactory
-工厂用创建新线程时,
nThreads <= 0
IllegalArgumentException
IOException
如果I/O错误发生
public static AsynchronousChannelGroup withCachedThreadPool(ExecutorService executor, int initialSize) throws IOException
这是一executor
参数创建新线程来执行提交处理I/O事件和集团对异步通道开始运营调度任务完成的结果ExecutorService
。它可能会重用以前构建的线程时,他们是可用的。
的initialSize
参数可以被实现为一个提示的任务,可将初始数。例如,它可以用来表示等待的I / O事件的初始线程数。
执行器的目的是专门使用所产生的异步信道组。在执行服务的有序shutdown
组结果终止。用其他方法关闭执行人服务结果在未指定的行为。
该集团是由调用openAsynchronousChannelGroup(ExecutorService,int)
方法的默认AsynchronousChannelProvider
对象。
executor
-结果组的线程池
initialSize
-价值
>=0
或具体实施违约负价值
IOException
如果I/O错误发生
Executors.newCachedThreadPool()
public static AsynchronousChannelGroup withThreadPool(ExecutorService executor) throws IOException
的executor
参数是一个ExecutorService
执行调度对集团的异步信道发起操作完成结果的任务。
配置执行器服务时应注意。它应该支持直接切换或无界提交任务队列和线程调用execute
方法不应该调用任务直接。一个实现可能要求额外的约束。
执行器的目的是专门使用所产生的异步信道组。在执行服务的有序shutdown
组结果终止。用其他方法关闭执行人服务结果在未指定的行为。
本集团通过与0
一initialSize
系统范围的默认AsynchronousChannelProvider
openAsynchronousChannelGroup(ExecutorService,int)
方法创建的对象。
executor
-结果组的线程池
IOException
如果I/O错误发生
public abstract boolean isShutdown()
true
异步信道组关机或已被标记为关闭。
public abstract boolean isTerminated()
这个方法返回true
,那么相关的线程池也terminated
。
true
public abstract void shutdown()
该方法标志着该组作为关闭。进一步尝试构建渠道,结合本集团将把ShutdownChannelGroupException
。当组中的所有异步通道关闭时,该组将终止,所有积极执行完成处理程序已运行完成,所有的资源都已被释放。如果该组已关闭,该方法将不会有任何效果。
public abstract void shutdownNow() throws IOException
除了由shutdown
方法执行的操作,该方法调用集团内所有开放通道close
方法。此方法不会试图停止或中断正在执行完成处理程序的线程。该组终止时,所有积极执行完成处理程序已运行完成,所有的资源已被释放。这种方法可能在任何时候被调用。如果一些其他线程已经调用它,那么另一个调用将阻塞,直到第一次调用完成,之后它将返回没有效果。
IOException
如果I/O错误发生
public abstract boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
此方法块,直到该组已终止,或超时发生,或当前线程被中断,以先发生的为准。
timeout
的最大等待时间,或零或更少不等待
unit
- timeout参数的时间单位
true
如果本集团已终止;
false
如果超时后终止
InterruptedException
如果中断等待
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.