{main: fixes

This commit is contained in:
Bruce Eckel 2016-07-07 17:22:39 -06:00
parent 60178b5d47
commit 6864a5ab36
3 changed files with 7 additions and 2 deletions

View File

@ -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.*;

View File

@ -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();
}
}

View File

@ -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.*;