以下测试程序执行简单的绑定/接受,然后关闭通道组。
我希望程序打印出生成的 ShutdownChannelGroupException,但它从不调用完成处理程序并在线程池中引发异常。
有人可以阐明发生了什么吗?当他们说 shutdownNow() 具有以下行为时,我是否误解了文档:“当所有主动执行的完成处理程序都运行完成并且所有资源都已释放时,该组终止”。
回调是不是“活动”还没有因此被丢弃?
public static void main(String[] args) throws java.io.IOException, InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
AsynchronousChannelGroup group = AsynchronousChannelGroup.withThreadPool(service);
AsynchronousServerSocketChannel acceptor = AsynchronousServerSocketChannel.open(group);
acceptor.bind(new InetSocketAddress(50000));
acceptor.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() {
@Override
public void completed(AsynchronousSocketChannel result, Object attachment) {
}
@Override
public void failed(Throwable exc, Object attachment) {
System.out.println("failed: " + exc.getMessage());
}
});
System.out.println("Shutting down");
group.shutdownNow();
System.out.println("Awaiting termination");
group.awaitTermination(60, TimeUnit.SECONDS);
System.out.println("Sleeping");
Thread.sleep(1000);
}