Loading src/main/java/ch15/ClosingTime.java +23 −33 Changes for src/main/java/ch15/ClosingTime.java: 23 added lines, 33 removed lines. Original line number Diff line number Diff line Loading @@ -12,10 +12,10 @@ public class ClosingTime { static void shutdownGracefully() { ExecutorService threadPool = Executors.newFixedThreadPool(2); threadPool.execute(() -> longJob("Long Job 1")); threadPool.execute(() -> shortJob("Short Job")); threadPool.execute(() -> shortJob("Long Job 2")); threadPool.execute(() -> shortJob("Shouldn't start job")); threadPool.execute(new LongJob("Long Job 1")); threadPool.execute(new ShortJob("Short Job")); threadPool.execute(new ShortJob("Long Job 2")); threadPool.execute(new ShortJob("Shouldn't start job")); threadPool.shutdown(); try { System.out.println("Finished? " + threadPool.awaitTermination(1, TimeUnit.SECONDS)); Loading @@ -26,42 +26,35 @@ public class ClosingTime { static void forceShutdown() { ExecutorService threadPool = Executors.newFixedThreadPool(2); threadPool.execute(() -> longJob("Long Job 1")); threadPool.execute(() -> shortJob("Short Job")); threadPool.execute(() -> longJob("Long Job 2")); threadPool.execute(() -> shortJob("Shouldn't start job")); threadPool.execute(new LongJob("Long Job 1")); threadPool.execute(new ShortJob("Short Job")); threadPool.execute(new LongJob("Long Job 2")); threadPool.execute(new ShortJob("Shouldn't start job")); List<Runnable> unfinished = threadPool.shutdownNow(); System.out.println("unfinished.size() = " + unfinished.size()); for (Runnable runnable : unfinished) { System.out.println(runnable); System.out.println(((NamedJob) runnable).getJobName()); } // try { // System.out.println("Finished? " + threadPool.awaitTermination(1, TimeUnit.SECONDS)); // } catch (InterruptedException e) { // e.printStackTrace(); // } } private static void longJob(String jobName) { // use sleep() to pretend like this is a long-running job try { TimeUnit.SECONDS.sleep(3); System.out.println(jobName); } catch (InterruptedException e) { e.printStackTrace(); } } private static void shortJob(String jobName) { System.out.println(jobName); abstract class NamedJob implements Runnable { protected String jobName; NamedJob(String jobName) { this.jobName = jobName; } public String getJobName() { return jobName; } } class ShortJob implements Runnable { private String jobName; class ShortJob extends NamedJob { ShortJob(String jobName) { this.jobName = jobName; super(jobName); } public void run() { Loading @@ -69,11 +62,9 @@ public class ClosingTime { } } class LongJob implements Runnable { private String jobName; class LongJob extends NamedJob { LongJob(String jobName) { this.jobName = jobName; super(jobName); } public void run() { Loading @@ -81,9 +72,8 @@ public class ClosingTime { TimeUnit.SECONDS.sleep(3); System.out.println(jobName); } catch (InterruptedException e) { System.out.println("Job interrupted: " + jobName); e.printStackTrace(); } } } } Loading
src/main/java/ch15/ClosingTime.java +23 −33 Changes for src/main/java/ch15/ClosingTime.java: 23 added lines, 33 removed lines. Original line number Diff line number Diff line Loading @@ -12,10 +12,10 @@ public class ClosingTime { static void shutdownGracefully() { ExecutorService threadPool = Executors.newFixedThreadPool(2); threadPool.execute(() -> longJob("Long Job 1")); threadPool.execute(() -> shortJob("Short Job")); threadPool.execute(() -> shortJob("Long Job 2")); threadPool.execute(() -> shortJob("Shouldn't start job")); threadPool.execute(new LongJob("Long Job 1")); threadPool.execute(new ShortJob("Short Job")); threadPool.execute(new ShortJob("Long Job 2")); threadPool.execute(new ShortJob("Shouldn't start job")); threadPool.shutdown(); try { System.out.println("Finished? " + threadPool.awaitTermination(1, TimeUnit.SECONDS)); Loading @@ -26,42 +26,35 @@ public class ClosingTime { static void forceShutdown() { ExecutorService threadPool = Executors.newFixedThreadPool(2); threadPool.execute(() -> longJob("Long Job 1")); threadPool.execute(() -> shortJob("Short Job")); threadPool.execute(() -> longJob("Long Job 2")); threadPool.execute(() -> shortJob("Shouldn't start job")); threadPool.execute(new LongJob("Long Job 1")); threadPool.execute(new ShortJob("Short Job")); threadPool.execute(new LongJob("Long Job 2")); threadPool.execute(new ShortJob("Shouldn't start job")); List<Runnable> unfinished = threadPool.shutdownNow(); System.out.println("unfinished.size() = " + unfinished.size()); for (Runnable runnable : unfinished) { System.out.println(runnable); System.out.println(((NamedJob) runnable).getJobName()); } // try { // System.out.println("Finished? " + threadPool.awaitTermination(1, TimeUnit.SECONDS)); // } catch (InterruptedException e) { // e.printStackTrace(); // } } private static void longJob(String jobName) { // use sleep() to pretend like this is a long-running job try { TimeUnit.SECONDS.sleep(3); System.out.println(jobName); } catch (InterruptedException e) { e.printStackTrace(); } } private static void shortJob(String jobName) { System.out.println(jobName); abstract class NamedJob implements Runnable { protected String jobName; NamedJob(String jobName) { this.jobName = jobName; } public String getJobName() { return jobName; } } class ShortJob implements Runnable { private String jobName; class ShortJob extends NamedJob { ShortJob(String jobName) { this.jobName = jobName; super(jobName); } public void run() { Loading @@ -69,11 +62,9 @@ public class ClosingTime { } } class LongJob implements Runnable { private String jobName; class LongJob extends NamedJob { LongJob(String jobName) { this.jobName = jobName; super(jobName); } public void run() { Loading @@ -81,9 +72,8 @@ public class ClosingTime { TimeUnit.SECONDS.sleep(3); System.out.println(jobName); } catch (InterruptedException e) { System.out.println("Job interrupted: " + jobName); e.printStackTrace(); } } } }