OnJava8-Examples/threads/FastSimulation.java

80 lines
2.6 KiB
Java
Raw Normal View History

2016-07-05 14:46:09 -06:00
// threads/FastSimulation.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
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];
2016-01-25 18:05:55 -08:00
static SplittableRandom rand = new SplittableRandom(47);
2015-06-15 17:47:35 -07:00
static class Evolver implements Runnable {
@Override
public void run() {
while(!Thread.interrupted()) {
// 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.
2015-12-02 09:20:27 -08:00
System.out.println(
"Old value changed from " + oldvalue);
2015-06-15 17:47:35 -07:00
}
}
}
}
}
2016-01-25 18:05:55 -08:00
public static void
main(String[] args) throws Exception {
ExecutorService es = Executors.newCachedThreadPool();
2015-06-15 17:47:35 -07:00
for(int i = 0; i < N_ELEMENTS; i++)
for(int j = 0; j < N_GENES; j++)
2016-01-25 18:05:55 -08:00
GRID[i][j] =
new AtomicInteger(rand.nextInt(1000));
2015-06-15 17:47:35 -07:00
for(int i = 0; i < N_EVOLVERS; i++)
2016-01-25 18:05:55 -08:00
es.execute(new Evolver());
2015-06-15 17:47:35 -07:00
TimeUnit.SECONDS.sleep(5);
2016-01-25 18:05:55 -08:00
es.shutdownNow();
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output: (First and last 10 Lines)
2015-06-15 17:47:35 -07:00
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
2015-09-07 11:44:36 -06:00
*/