@@ -46,26 +46,23 @@ public class HorseRace {
4646 Executors .newCachedThreadPool ();
4747 private CyclicBarrier barrier ;
4848 public HorseRace (int nHorses , final int pause ) {
49- barrier = new CyclicBarrier (nHorses , new Runnable () {
50- @ Override
51- public void run () {
52- StringBuilder s = new StringBuilder ();
53- for (int i = 0 ; i < FINISH_LINE ; i ++)
54- s .append ("=" ); // The fence on the racetrack
55- print (s );
56- for (Horse horse : horses )
57- print (horse .tracks ());
58- for (Horse horse : horses )
59- if (horse .getStrides () >= FINISH_LINE ) {
60- print (horse + "won!" );
61- exec .shutdownNow ();
62- return ;
63- }
64- try {
65- TimeUnit .MILLISECONDS .sleep (pause );
66- } catch (InterruptedException e ) {
67- print ("barrier-action sleep interrupted" );
49+ barrier = new CyclicBarrier (nHorses , () -> {
50+ StringBuilder s = new StringBuilder ();
51+ for (int i = 0 ; i < FINISH_LINE ; i ++)
52+ s .append ("=" ); // The fence on the racetrack
53+ print (s );
54+ for (Horse horse : horses )
55+ print (horse .tracks ());
56+ for (Horse horse : horses )
57+ if (horse .getStrides () >= FINISH_LINE ) {
58+ print (horse + "won!" );
59+ exec .shutdownNow ();
60+ return ;
6861 }
62+ try {
63+ TimeUnit .MILLISECONDS .sleep (pause );
64+ } catch (InterruptedException e ) {
65+ print ("barrier-action sleep interrupted" );
6966 }
7067 });
7168 for (int i = 0 ; i < nHorses ; i ++) {
0 commit comments