{main: fixes
This commit is contained in:
parent
60178b5d47
commit
6864a5ab36
@ -6,6 +6,7 @@
|
|||||||
// Synchronizing blocks instead of entire methods. Also
|
// Synchronizing blocks instead of entire methods. Also
|
||||||
// demonstrates protection of a non-thread-safe class
|
// demonstrates protection of a non-thread-safe class
|
||||||
// with a thread-safe one.
|
// with a thread-safe one.
|
||||||
|
// {main: threads.CriticalSection}
|
||||||
package threads;
|
package threads;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.*;
|
import java.util.concurrent.atomic.*;
|
||||||
|
@ -13,10 +13,12 @@ public class FastSimulation {
|
|||||||
static final AtomicInteger[][] GRID =
|
static final AtomicInteger[][] GRID =
|
||||||
new AtomicInteger[N_ELEMENTS][N_GENES];
|
new AtomicInteger[N_ELEMENTS][N_GENES];
|
||||||
static SplittableRandom rand = new SplittableRandom(47);
|
static SplittableRandom rand = new SplittableRandom(47);
|
||||||
|
private static volatile boolean running = true;
|
||||||
|
public static void stop() { running = false; }
|
||||||
static class Evolver implements Runnable {
|
static class Evolver implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while(!Thread.interrupted()) {
|
while(running) {
|
||||||
// Randomly select an element to work on:
|
// Randomly select an element to work on:
|
||||||
int element = rand.nextInt(N_ELEMENTS);
|
int element = rand.nextInt(N_ELEMENTS);
|
||||||
for(int i = 0; i < N_GENES; i++) {
|
for(int i = 0; i < N_GENES; i++) {
|
||||||
@ -50,7 +52,8 @@ public class FastSimulation {
|
|||||||
new AtomicInteger(rand.nextInt(1000));
|
new AtomicInteger(rand.nextInt(1000));
|
||||||
for(int i = 0; i < N_EVOLVERS; i++)
|
for(int i = 0; i < N_EVOLVERS; i++)
|
||||||
es.execute(new Evolver());
|
es.execute(new Evolver());
|
||||||
TimeUnit.SECONDS.sleep(5);
|
TimeUnit.SECONDS.sleep(4);
|
||||||
|
FastSimulation.stop();
|
||||||
es.shutdownNow();
|
es.shutdownNow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
// We make no guarantees that this code is fit for any purpose.
|
// We make no guarantees that this code is fit for any purpose.
|
||||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||||
// {Args: 5}
|
// {Args: 5}
|
||||||
|
// {main: threads.restaurant2.RestaurantWithQueues}
|
||||||
package threads.restaurant2;
|
package threads.restaurant2;
|
||||||
import enums.menu.*;
|
import enums.menu.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user