Commit 245d50b7 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] More shutdown examples

parent 7ac550fc
Loading
Loading
Loading
Loading
+25 −0
Changes for src/main/java/ch15/ClosingTime.java: 25 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -27,6 +27,31 @@ public class ClosingTime {
    System.out.println("threadPool.isShutdown() = " + threadPool.isShutdown());
    System.out.println("threadPool.isTerminated() = " + threadPool.isTerminated());

    try {
      System.out.println("Finished? " + threadPool.awaitTermination(100, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("threadPool.isShutdown() = " + threadPool.isShutdown());
    System.out.println("threadPool.isTerminated() = " + threadPool.isTerminated());
  }

  static void shutdownGracefullyWorkstealing() {
    ExecutorService threadPool = Executors.newWorkStealingPool(2);
    threadPool.execute(new LongJob("Long Job 1"));
    threadPool.execute(new ShortJob("Short Job"));
    threadPool.execute(new LongJob("Long Job 2"));
    threadPool.execute(new ShortJob("Queued job"));
    threadPool.shutdown();
    ShortJob tooLateJob = new ShortJob("Too late job");
    try {
      threadPool.execute(tooLateJob);
    } catch (RejectedExecutionException e) {
      System.out.println("Too late to start another job!! " + tooLateJob);
    }
    System.out.println("threadPool.isShutdown() = " + threadPool.isShutdown());
    System.out.println("threadPool.isTerminated() = " + threadPool.isTerminated());

    try {
      System.out.println("Finished? " + threadPool.awaitTermination(4, TimeUnit.SECONDS));
    } catch (InterruptedException e) {