Commit 6f5a1c5f authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] Using shutdown and shutdownNow

parent 245d50b7
Loading
Loading
Loading
Loading
+28 −2
Changes for src/main/java/ch15/ClosingTime.java: 28 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -7,8 +7,34 @@ import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;

public class ClosingTime {
  public static void main(String[] args) {
    shutdownGracefully();
  public static void main(String[] args) throws InterruptedException {
    fullShutdown();
  }

  static void fullShutdown() throws InterruptedException {
    ExecutorService threadPool = Executors.newFixedThreadPool(2);
    threadPool.execute(new LongJob("Long Job 1"));
    threadPool.execute(new ShortJob("Short Job"));
    threadPool.execute(new LongJob("Long Job 2"));
    threadPool.execute(new LongJob("Long Job 3"));
    threadPool.execute(new LongJob("Long Job 4"));
    threadPool.execute(new ShortJob("Shouldn't start 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);
    }
    threadPool.awaitTermination(4, TimeUnit.SECONDS);

    List<Runnable> unfinished = threadPool.shutdownNow();

    System.out.println("unfinished.size() = " + unfinished.size());
    for (Runnable runnable : unfinished) {
      System.out.println(runnable);
    }

  }

  static void shutdownGracefully() {