diff --git a/threads/FastSimulation.java b/threads/FastSimulation.java deleted file mode 100644 index f0af5dbe..00000000 --- a/threads/FastSimulation.java +++ /dev/null @@ -1,82 +0,0 @@ -// threads/FastSimulation.java -// (c)2016 MindView LLC: see Copyright.txt -// We make no guarantees that this code is fit for any purpose. -// Visit http://mindviewinc.com/Books/OnJava/ for more book information. -import java.util.concurrent.*; -import java.util.concurrent.atomic.*; -import java.util.*; - -public class FastSimulation { - static final int N_ELEMENTS = 100000; - static final int N_GENES = 30; - static final int N_EVOLVERS = 50; - 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(running) { - // Randomly select an element to work on: - int element = rand.nextInt(N_ELEMENTS); - for(int i = 0; i < N_GENES; i++) { - int previous = element - 1; - if(previous < 0) previous = N_ELEMENTS - 1; - int next = element + 1; - if(next >= N_ELEMENTS) next = 0; - int oldvalue = GRID[element][i].get(); - // Perform some kind of modeling calculation: - int newvalue = oldvalue + - GRID[previous][i].get() + GRID[next][i].get(); - newvalue /= 3; // Average the three values - if(!GRID[element][i] - .compareAndSet(oldvalue, newvalue)) { - // Policy here to deal with failure. Here, we - // just report it and ignore it; our model - // will eventually deal with it. - System.out.println( - "Old value changed from " + oldvalue); - } - } - } - } - } - public static void - main(String[] args) throws Exception { - ExecutorService es = Executors.newCachedThreadPool(); - for(int i = 0; i < N_ELEMENTS; i++) - for(int j = 0; j < N_GENES; j++) - GRID[i][j] = - new AtomicInteger(rand.nextInt(1000)); - for(int i = 0; i < N_EVOLVERS; i++) - es.execute(new Evolver()); - TimeUnit.SECONDS.sleep(4); - FastSimulation.stop(); - es.shutdownNow(); - } -} -/* Output: (First and last 10 Lines) -Old value changed from 542 -Old value changed from 447 -Old value changed from 446 -Old value changed from 643 -Old value changed from 419 -Old value changed from 573 -Old value changed from 668 -Old value changed from 710 -Old value changed from 800 -Old value changed from 406 -________...________...________...________...________ -Old value changed from 458 -Old value changed from 436 -Old value changed from 475 -Old value changed from 501 -Old value changed from 526 -Old value changed from 465 -Old value changed from 467 -Old value changed from 368 -Old value changed from 404 -Old value changed from 428 -*/ diff --git a/threads/ThreadLocalVariableHolder.java b/threads/ThreadLocalVariableHolder.java deleted file mode 100644 index 2df98df5..00000000 --- a/threads/ThreadLocalVariableHolder.java +++ /dev/null @@ -1,71 +0,0 @@ -// threads/ThreadLocalVariableHolder.java -// (c)2016 MindView LLC: see Copyright.txt -// We make no guarantees that this code is fit for any purpose. -// Visit http://mindviewinc.com/Books/OnJava/ for more book information. -// Automatically giving each thread its own storage -import java.util.concurrent.*; -import java.util.*; - -class Accessor implements Runnable { - private final int id; - public Accessor(int idn) { id = idn; } - @Override - public void run() { - while(!Thread.currentThread().isInterrupted()) { - ThreadLocalVariableHolder.increment(); - System.out.println(this); - Thread.yield(); - } - } - @Override - public String toString() { - return "#" + id + ": " + - ThreadLocalVariableHolder.get(); - } -} - -public class ThreadLocalVariableHolder { - private static ThreadLocal value = - new ThreadLocal() { - private SplittableRandom rand = new SplittableRandom(47); - @Override - protected synchronized Integer initialValue() { - return rand.nextInt(10000); - } - }; - public static void increment() { - value.set(value.get() + 1); - } - public static int get() { return value.get(); } - public static void - main(String[] args) throws Exception { - ExecutorService es = Executors.newCachedThreadPool(); - for(int i = 0; i < 5; i++) - es.execute(new Accessor(i)); - TimeUnit.SECONDS.sleep(3); // Run for a while - es.shutdownNow(); // All Accessors will quit - } -} -/* Output: (First and last 10 Lines) -#2: 9259 -#1: 962 -#3: 1862 -#4: 6694 -#0: 556 -#4: 6695 -#4: 6696 -#3: 1863 -#1: 963 -#2: 9260 -________...________...________...________...________ -#3: 3791 -#2: 11096 -#4: 8402 -#4: 8403 -#2: 11097 -#3: 3792 -#0: 2637 -#1: 3032 -#4: 8404 -#2: 11098 -*/