ShutdownChannelGroupException when perform Future.get

Li Li fancyerii at gmail.com
Sat Jan 11 06:35:34 PST 2014


hi all,
    I am test an example of Pro Java 7 NIO.2 But it throws exception

java.util.concurrent.ExecutionException:
java.nio.channels.ShutdownChannelGroupException
at java.util.concurrent.FutureTask.report(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at ch09.ExecutorTest.main(ExecutorTest.java:63)
Caused by: java.nio.channels.ShutdownChannelGroupException
at sun.nio.ch.Invoker.invokeOnThreadInThreadPool(Unknown Source)
at sun.nio.ch.WindowsAsynchronousFileChannelImpl.implRead(Unknown Source)
at sun.nio.ch.AsynchronousFileChannelImpl.read(Unknown Source)
at ch09.ExecutorTest$1.call(ExecutorTest.java:43)
at ch09.ExecutorTest$1.call(ExecutorTest.java:1)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

------------source code-----------

public class ExecutorTest {
    private static Set<StandardOpenOption> withOptions() {
    final Set<StandardOpenOption> options = new TreeSet<>();
    options.add(StandardOpenOption.READ);
return options;
}

public static void main(String[] args) {
    final int THREADS = 5;
    ExecutorService taskExecutor = Executors.newFixedThreadPool(THREADS);
    String encoding = System.getProperty("file.encoding");
    List<Future<ByteBuffer>> list = new ArrayList<>();
    int sheeps = 0;
    Path path = Paths.get("e:", "highlighted.html");
    try (AsynchronousFileChannel asynchronousFileChannel =
AsynchronousFileChannel
        .open(path, withOptions(), taskExecutor)) {
        for (int i = 0; i < 50; i++) {
            Callable<ByteBuffer> worker = new Callable<ByteBuffer>() {
                @Override
                public ByteBuffer call() throws Exception {
                    ByteBuffer buffer =
ByteBuffer.allocateDirect(ThreadLocalRandom.current()
                        .nextInt(100, 200));
                    asynchronousFileChannel.read(buffer, ThreadLocalRandom
                        .current().nextInt(0, 100));
                    return buffer;
                }
            };
            Future<ByteBuffer> future = taskExecutor.submit(worker);
            list.add(future);
        }
        // this will make the executor accept no new threads
        // and finish all existing threads in the queue
        taskExecutor.shutdown();
        // wait until all threads are finished
        while (!taskExecutor.isTerminated()) {
            // do something else while the buffers are prepared
            System.out.println("Counting sheep while filling up some
buffers! So far I counted: "
            + (sheeps += 1));
        }

        System.out.println("\nDone! Here are the buffers:\n");
        for (Future<ByteBuffer> future : list) {
            ByteBuffer buffer = future.get();
            System.out.println("\n\n" + buffer);
            System.out

.println("______________________________________________________");
            buffer.flip();
            System.out.print(Charset.forName(encoding).decode(buffer));
            buffer.clear();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
}


More information about the nio-discuss mailing list