From 6864a5ab361a952714bf7c5a9b7362ebfe34cb9b Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Thu, 7 Jul 2016 17:22:39 -0600 Subject: [PATCH] {main: fixes --- threads/CriticalSection.java | 1 + threads/FastSimulation.java | 7 +++++-- threads/restaurant2/RestaurantWithQueues.java | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/threads/CriticalSection.java b/threads/CriticalSection.java index 67f99cd0..922a9e03 100644 --- a/threads/CriticalSection.java +++ b/threads/CriticalSection.java @@ -6,6 +6,7 @@ // Synchronizing blocks instead of entire methods. Also // demonstrates protection of a non-thread-safe class // with a thread-safe one. +// {main: threads.CriticalSection} package threads; import java.util.concurrent.*; import java.util.concurrent.atomic.*; diff --git a/threads/FastSimulation.java b/threads/FastSimulation.java index 1eec612f..f0af5dbe 100644 --- a/threads/FastSimulation.java +++ b/threads/FastSimulation.java @@ -13,10 +13,12 @@ public class FastSimulation { static final AtomicInteger[][] GRID = new AtomicInteger[N_ELEMENTS][N_GENES]; static SplittableRandom rand = new SplittableRandom(47); + private static volatile boolean running = true; + public static void stop() { running = false; } static class Evolver implements Runnable { @Override public void run() { - while(!Thread.interrupted()) { + while(running) { // Randomly select an element to work on: int element = rand.nextInt(N_ELEMENTS); for(int i = 0; i < N_GENES; i++) { @@ -50,7 +52,8 @@ public class FastSimulation { new AtomicInteger(rand.nextInt(1000)); for(int i = 0; i < N_EVOLVERS; i++) es.execute(new Evolver()); - TimeUnit.SECONDS.sleep(5); + TimeUnit.SECONDS.sleep(4); + FastSimulation.stop(); es.shutdownNow(); } } diff --git a/threads/restaurant2/RestaurantWithQueues.java b/threads/restaurant2/RestaurantWithQueues.java index 195f4eed..85ebf032 100644 --- a/threads/restaurant2/RestaurantWithQueues.java +++ b/threads/restaurant2/RestaurantWithQueues.java @@ -3,6 +3,7 @@ // We make no guarantees that this code is fit for any purpose. // Visit http://mindviewinc.com/Books/OnJava/ for more book information. // {Args: 5} +// {main: threads.restaurant2.RestaurantWithQueues} package threads.restaurant2; import enums.menu.*; import java.util.concurrent.*;