Skip to content

Commit 8833d2e

Browse files
author
Bruce Eckel
committed
Name changes and new TimedAbort
1 parent d3ca481 commit 8833d2e

11 files changed

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
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
77
import 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
}

lowlevel/EvenChecker.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
import java.util.concurrent.*;
6+
import onjava.TimedAbort;
67

78
public 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 numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
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:
2220
Press 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
*/

lowlevel/ExceptionThread.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

lowlevel/IntGenerator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

lowlevel/IntSupplier.java

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
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
77
import 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 numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
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

99
public 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
}

lowlevel/TestAbort.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
*/

lowlevel/WorkStealingPool.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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.*;
56
import java.util.concurrent.*;
67

78
class 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
}

0 commit comments

Comments
 (0)