2016-01-25 18:05:55 -08:00
|
|
|
// tasks/HorseRace.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.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Using CyclicBarriers
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.util.concurrent.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
class Horse implements Runnable {
|
|
|
|
private static int counter = 0;
|
|
|
|
private final int id = counter++;
|
|
|
|
private int strides = 0;
|
2016-01-25 18:05:55 -08:00
|
|
|
private static SplittableRandom rand = new SplittableRandom(47);
|
2015-06-15 17:47:35 -07:00
|
|
|
private static CyclicBarrier barrier;
|
|
|
|
public Horse(CyclicBarrier b) { barrier = b; }
|
|
|
|
public synchronized int getStrides() { return strides; }
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
while(!Thread.interrupted()) {
|
|
|
|
synchronized(this) {
|
2016-01-25 18:05:55 -08:00
|
|
|
strides += rand.nextInt(3); // Yeilds 0, 1 or 2
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
barrier.await();
|
|
|
|
}
|
|
|
|
} catch(InterruptedException e) {
|
|
|
|
// A legitimate way to exit
|
|
|
|
} catch(BrokenBarrierException e) {
|
|
|
|
// This one we want to know about
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String toString() { return "Horse " + id + " "; }
|
|
|
|
public String tracks() {
|
|
|
|
StringBuilder s = new StringBuilder();
|
|
|
|
for(int i = 0; i < getStrides(); i++)
|
|
|
|
s.append("*");
|
|
|
|
s.append(id);
|
|
|
|
return s.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class HorseRace {
|
|
|
|
static final int FINISH_LINE = 75;
|
|
|
|
private List<Horse> horses = new ArrayList<>();
|
|
|
|
private ExecutorService exec =
|
|
|
|
Executors.newCachedThreadPool();
|
|
|
|
private CyclicBarrier barrier;
|
|
|
|
public HorseRace(int nHorses, final int pause) {
|
|
|
|
barrier = new CyclicBarrier(nHorses, () -> {
|
|
|
|
StringBuilder s = new StringBuilder();
|
|
|
|
for(int i = 0; i < FINISH_LINE; i++)
|
|
|
|
s.append("="); // The fence on the racetrack
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(s);
|
2015-06-15 17:47:35 -07:00
|
|
|
for(Horse horse : horses)
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(horse.tracks());
|
2015-06-15 17:47:35 -07:00
|
|
|
for(Horse horse : horses)
|
|
|
|
if(horse.getStrides() >= FINISH_LINE) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(horse + "won!");
|
2015-06-15 17:47:35 -07:00
|
|
|
exec.shutdownNow();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
TimeUnit.MILLISECONDS.sleep(pause);
|
|
|
|
} catch(InterruptedException e) {
|
2015-12-02 09:20:27 -08:00
|
|
|
System.out.println(
|
|
|
|
"barrier-action sleep interrupted");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
for(int i = 0; i < nHorses; i++) {
|
|
|
|
Horse horse = new Horse(barrier);
|
|
|
|
horses.add(horse);
|
|
|
|
exec.execute(horse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
int nHorses = 7;
|
|
|
|
int pause = 200;
|
|
|
|
if(args.length > 0) { // Optional argument
|
|
|
|
int n = new Integer(args[0]);
|
|
|
|
nHorses = n > 0 ? n : nHorses;
|
|
|
|
}
|
|
|
|
if(args.length > 1) { // Optional argument
|
|
|
|
int p = new Integer(args[1]);
|
|
|
|
pause = p > -1 ? p : pause;
|
|
|
|
}
|
|
|
|
new HorseRace(nHorses, pause);
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output: (First and last 18 Lines)
|
2015-06-15 17:47:35 -07:00
|
|
|
===========================================================
|
|
|
|
================
|
|
|
|
*0
|
|
|
|
**1
|
|
|
|
*2
|
|
|
|
**3
|
|
|
|
**4
|
|
|
|
*5
|
|
|
|
**6
|
|
|
|
===========================================================
|
|
|
|
================
|
|
|
|
***0
|
|
|
|
**1
|
|
|
|
*2
|
|
|
|
***3
|
|
|
|
***4
|
|
|
|
*5
|
|
|
|
**6
|
|
|
|
===========================================================
|
|
|
|
================
|
|
|
|
*****0
|
|
|
|
________...________...________...________...________
|
|
|
|
*****************************************************6
|
|
|
|
===========================================================
|
|
|
|
================
|
|
|
|
***********************************************************
|
|
|
|
*0
|
|
|
|
***********************************************************
|
|
|
|
1
|
|
|
|
***********************************************************
|
|
|
|
*2
|
|
|
|
***********************************************************
|
|
|
|
**************3
|
|
|
|
***********************************************************
|
|
|
|
4
|
|
|
|
***********************************************************
|
|
|
|
***********5
|
|
|
|
*******************************************************6
|
|
|
|
===========================================================
|
|
|
|
================
|
|
|
|
***********************************************************
|
|
|
|
**0
|
|
|
|
***********************************************************
|
|
|
|
**1
|
|
|
|
***********************************************************
|
|
|
|
**2
|
|
|
|
***********************************************************
|
|
|
|
****************3
|
|
|
|
***********************************************************
|
|
|
|
*4
|
|
|
|
***********************************************************
|
|
|
|
*************5
|
|
|
|
********************************************************6
|
|
|
|
Horse 3 won!
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|