File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- // lowlevel/AtomicEvenSupplier .java
1+ // lowlevel/AtomicEvenProducer .java
22// (c)2017 MindView LLC: see Copyright.txt
33// We make no guarantees that this code is fit for any purpose.
44// Visit http://OnJava8.com for more book information.
55// Atomic classes are occasionally useful in regular code
66// {IgnoreOutput} // No output validation
77import java .util .concurrent .atomic .*;
8- import onjava .TimedAbort ;
98
10- public class AtomicEvenSupplier extends IntSupplier {
9+ public class AtomicEvenProducer extends IntGenerator {
1110 private AtomicInteger currentEvenValue =
1211 new AtomicInteger (0 );
1312 @ Override
1413 public int next () {
1514 return currentEvenValue .addAndGet (2 );
1615 }
1716 public static void main (String [] args ) {
18- new TimedAbort (4 );
19- EvenChecker .test (new AtomicEvenSupplier ());
17+ EvenChecker .test (new AtomicEvenProducer ());
2018 }
2119}
Original file line number Diff line number Diff line change 33// We make no guarantees that this code is fit for any purpose.
44// Visit http://OnJava8.com for more book information.
55import java .util .concurrent .*;
6+ import onjava .TimedAbort ;
67
78public class EvenChecker implements Runnable {
8- private IntSupplier generator ;
9+ private IntGenerator generator ;
910 private final int id ;
10- public EvenChecker (IntSupplier g , int ident ) {
11- generator = g ;
12- id = ident ;
11+ public EvenChecker (IntGenerator generator , int id ) {
12+ this . generator = generator ;
13+ this . id = id ;
1314 }
1415 @ Override
1516 public void run () {
@@ -21,16 +22,17 @@ public void run() {
2122 }
2223 }
2324 }
24- // Test any type of IntSupplier :
25- public static void test (IntSupplier gp , int count ) {
25+ // Test any IntGenerator :
26+ public static void test (IntGenerator gp , int count ) {
2627 System .out .println ("Press Control-C to exit" );
2728 ExecutorService es = Executors .newCachedThreadPool ();
2829 for (int i = 0 ; i < count ; i ++)
2930 es .execute (new EvenChecker (gp , i ));
3031 es .shutdown ();
3132 }
3233 // Default value for count:
33- public static void test (IntSupplier gp ) {
34+ public static void test (IntGenerator gp ) {
35+ new TimedAbort (4 );
3436 test (gp , 10 );
3537 }
3638}
Original file line number Diff line number Diff line change 1- // lowlevel/EvenSupplier .java
1+ // lowlevel/EvenProducer .java
22// (c)2017 MindView LLC: see Copyright.txt
33// We make no guarantees that this code is fit for any purpose.
44// Visit http://OnJava8.com for more book information.
55// When threads collide
6- import onjava .TimedAbort ;
76
8- public class EvenSupplier extends IntSupplier {
7+ public class EvenProducer extends IntGenerator {
98 private int currentEvenValue = 0 ;
109 @ Override
1110 public int next () {
12- ++currentEvenValue ; // Danger point here!
11+ ++currentEvenValue ; // [1]
1312 ++currentEvenValue ;
1413 return currentEvenValue ;
1514 }
1615 public static void main (String [] args ) {
17- new TimedAbort (4 );
18- EvenChecker .test (new EvenSupplier ());
16+ EvenChecker .test (new EvenProducer ());
1917 }
2018}
2119/* Output:
2220Press Control-C to exit
23- 193 not even!
24- 191 not even!
25- TimedAbort 4
21+ 841 not even!
22+ 847 not even!
23+ 845 not even!
24+ 843 not even!
2625*/
Original file line number Diff line number Diff line change @@ -12,7 +12,8 @@ public void run() {
1212 throw new RuntimeException ();
1313 }
1414 public static void main (String [] args ) {
15- ExecutorService es = Executors .newCachedThreadPool ();
15+ ExecutorService es =
16+ Executors .newCachedThreadPool ();
1617 es .execute (new ExceptionThread ());
1718 es .shutdown ();
1819 }
Original file line number Diff line number Diff line change 1+ // lowlevel/IntGenerator.java
2+ // (c)2017 MindView LLC: see Copyright.txt
3+ // We make no guarantees that this code is fit for any purpose.
4+ // Visit http://OnJava8.com for more book information.
5+ import java .util .concurrent .atomic .AtomicBoolean ;
6+
7+ public abstract class IntGenerator {
8+ private AtomicBoolean canceled =
9+ new AtomicBoolean ();
10+ public abstract int next ();
11+ public void cancel () { canceled .set (true ); }
12+ public boolean isCanceled () {
13+ return canceled .get ();
14+ }
15+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1- // lowlevel/MutexEvenSupplier .java
1+ // lowlevel/MutexEvenProducer .java
22// (c)2017 MindView LLC: see Copyright.txt
33// We make no guarantees that this code is fit for any purpose.
44// Visit http://OnJava8.com for more book information.
55// Preventing thread collisions with mutexes
66// {IgnoreOutput} // No output validation
77import java .util .concurrent .locks .*;
8- import onjava .TimedAbort ;
98
10- public class MutexEvenSupplier extends IntSupplier {
9+ public class MutexEvenProducer extends IntGenerator {
1110 private int currentEvenValue = 0 ;
1211 private Lock lock = new ReentrantLock ();
1312 @ Override
@@ -23,7 +22,6 @@ public int next() {
2322 }
2423 }
2524 public static void main (String [] args ) {
26- new TimedAbort (4 );
27- EvenChecker .test (new MutexEvenSupplier ());
25+ EvenChecker .test (new MutexEvenProducer ());
2826 }
2927}
Original file line number Diff line number Diff line change 1- // lowlevel/SynchronizedEvenSupplier .java
1+ // lowlevel/SynchronizedEvenProducer .java
22// (c)2017 MindView LLC: see Copyright.txt
33// We make no guarantees that this code is fit for any purpose.
44// Visit http://OnJava8.com for more book information.
55// Simplifying mutexes with the synchronized keyword
66// {IgnoreOutput} // No output validation
7- import onjava .TimedAbort ;
7+ import onjava .Nap ;
88
99public class
10- SynchronizedEvenSupplier extends IntSupplier {
10+ SynchronizedEvenProducer extends IntGenerator {
1111 private int currentEvenValue = 0 ;
1212 @ Override
1313 public synchronized int next () {
1414 ++currentEvenValue ;
15- Thread . yield ( ); // Cause failure faster
15+ new Nap ( 10 ); // Cause failure faster
1616 ++currentEvenValue ;
1717 return currentEvenValue ;
1818 }
1919 public static void main (String [] args ) {
20- new TimedAbort (4 );
21- EvenChecker .test (new SynchronizedEvenSupplier ());
20+ EvenChecker .test (new SynchronizedEvenProducer ());
2221 }
2322}
Original file line number Diff line number Diff line change 1+ // lowlevel/TestAbort.java
2+ // (c)2017 MindView LLC: see Copyright.txt
3+ // We make no guarantees that this code is fit for any purpose.
4+ // Visit http://OnJava8.com for more book information.
5+ import onjava .*;
6+
7+ public class TestAbort {
8+ public static void main (String [] args ) {
9+ new TimedAbort (1 );
10+ System .out .println ("Napping for 4" );
11+ new Nap (4000 );
12+ }
13+ }
14+ /* Output:
15+ Napping for 4
16+ TimedAbort 1
17+ */
Original file line number Diff line number Diff line change 22// (c)2017 MindView LLC: see Copyright.txt
33// We make no guarantees that this code is fit for any purpose.
44// Visit http://OnJava8.com for more book information.
5+ import java .util .stream .*;
56import java .util .concurrent .*;
67
78class ShowThread implements Runnable {
@@ -19,8 +20,9 @@ public static void main(String[] args)
1920 Runtime .getRuntime ().availableProcessors ());
2021 ExecutorService exec =
2122 Executors .newWorkStealingPool ();
22- for (int i = 0 ; i < 10 ; i ++)
23- exec .execute (new ShowThread ());
23+ IntStream .range (0 , 10 )
24+ .mapToObj (n -> new ShowThread ())
25+ .forEach (exec ::execute );
2426 exec .awaitTermination (1 , TimeUnit .SECONDS );
2527 }
2628}
You can’t perform that action at this time.
0 commit comments