Name changes and new TimedAbort

This commit is contained in:
Bruce Eckel 2017-01-11 16:18:57 -08:00
parent d3ca481bdb
commit 8833d2e228
11 changed files with 72 additions and 55 deletions

View File

@ -1,13 +1,12 @@
// lowlevel/AtomicEvenSupplier.java
// lowlevel/AtomicEvenProducer.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Atomic classes are occasionally useful in regular code
// {IgnoreOutput} // No output validation
import java.util.concurrent.atomic.*;
import onjava.TimedAbort;
public class AtomicEvenSupplier extends IntSupplier {
public class AtomicEvenProducer extends IntGenerator {
private AtomicInteger currentEvenValue =
new AtomicInteger(0);
@Override
@ -15,7 +14,6 @@ public class AtomicEvenSupplier extends IntSupplier {
return currentEvenValue.addAndGet(2);
}
public static void main(String[] args) {
new TimedAbort(4);
EvenChecker.test(new AtomicEvenSupplier());
EvenChecker.test(new AtomicEvenProducer());
}
}

View File

@ -3,13 +3,14 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.concurrent.*;
import onjava.TimedAbort;
public class EvenChecker implements Runnable {
private IntSupplier generator;
private IntGenerator generator;
private final int id;
public EvenChecker(IntSupplier g, int ident) {
generator = g;
id = ident;
public EvenChecker(IntGenerator generator, int id) {
this.generator = generator;
this.id = id;
}
@Override
public void run() {
@ -21,8 +22,8 @@ public class EvenChecker implements Runnable {
}
}
}
// Test any type of IntSupplier:
public static void test(IntSupplier gp, int count) {
// Test any IntGenerator:
public static void test(IntGenerator gp, int count) {
System.out.println("Press Control-C to exit");
ExecutorService es = Executors.newCachedThreadPool();
for(int i = 0; i < count; i++)
@ -30,7 +31,8 @@ public class EvenChecker implements Runnable {
es.shutdown();
}
// Default value for count:
public static void test(IntSupplier gp) {
public static void test(IntGenerator gp) {
new TimedAbort(4);
test(gp, 10);
}
}

View File

@ -1,26 +1,25 @@
// lowlevel/EvenSupplier.java
// lowlevel/EvenProducer.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// When threads collide
import onjava.TimedAbort;
public class EvenSupplier extends IntSupplier {
public class EvenProducer extends IntGenerator {
private int currentEvenValue = 0;
@Override
public int next() {
++currentEvenValue; // Danger point here!
++currentEvenValue; // [1]
++currentEvenValue;
return currentEvenValue;
}
public static void main(String[] args) {
new TimedAbort(4);
EvenChecker.test(new EvenSupplier());
EvenChecker.test(new EvenProducer());
}
}
/* Output:
Press Control-C to exit
193 not even!
191 not even!
TimedAbort 4
841 not even!
847 not even!
845 not even!
843 not even!
*/

View File

@ -12,7 +12,8 @@ public class ExceptionThread implements Runnable {
throw new RuntimeException();
}
public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();
ExecutorService es =
Executors.newCachedThreadPool();
es.execute(new ExceptionThread());
es.shutdown();
}

View File

@ -0,0 +1,15 @@
// lowlevel/IntGenerator.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.concurrent.atomic.AtomicBoolean;
public abstract class IntGenerator {
private AtomicBoolean canceled =
new AtomicBoolean();
public abstract int next();
public void cancel() { canceled.set(true); }
public boolean isCanceled() {
return canceled.get();
}
}

View File

@ -1,12 +0,0 @@
// lowlevel/IntSupplier.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
public abstract class IntSupplier {
private volatile boolean canceled = false;
public abstract int next();
// Allow this to be canceled:
public void cancel() { canceled = true; }
public boolean isCanceled() { return canceled; }
}

View File

@ -1,13 +1,12 @@
// lowlevel/MutexEvenSupplier.java
// lowlevel/MutexEvenProducer.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Preventing thread collisions with mutexes
// {IgnoreOutput} // No output validation
import java.util.concurrent.locks.*;
import onjava.TimedAbort;
public class MutexEvenSupplier extends IntSupplier {
public class MutexEvenProducer extends IntGenerator {
private int currentEvenValue = 0;
private Lock lock = new ReentrantLock();
@Override
@ -23,7 +22,6 @@ public class MutexEvenSupplier extends IntSupplier {
}
}
public static void main(String[] args) {
new TimedAbort(4);
EvenChecker.test(new MutexEvenSupplier());
EvenChecker.test(new MutexEvenProducer());
}
}

View File

@ -1,23 +1,22 @@
// lowlevel/SynchronizedEvenSupplier.java
// lowlevel/SynchronizedEvenProducer.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Simplifying mutexes with the synchronized keyword
// {IgnoreOutput} // No output validation
import onjava.TimedAbort;
import onjava.Nap;
public class
SynchronizedEvenSupplier extends IntSupplier {
SynchronizedEvenProducer extends IntGenerator {
private int currentEvenValue = 0;
@Override
public synchronized int next() {
++currentEvenValue;
Thread.yield(); // Cause failure faster
new Nap(10); // Cause failure faster
++currentEvenValue;
return currentEvenValue;
}
public static void main(String[] args) {
new TimedAbort(4);
EvenChecker.test(new SynchronizedEvenSupplier());
EvenChecker.test(new SynchronizedEvenProducer());
}
}

17
lowlevel/TestAbort.java Normal file
View File

@ -0,0 +1,17 @@
// lowlevel/TestAbort.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import onjava.*;
public class TestAbort {
public static void main(String[] args) {
new TimedAbort(1);
System.out.println("Napping for 4");
new Nap(4000);
}
}
/* Output:
Napping for 4
TimedAbort 1
*/

View File

@ -2,6 +2,7 @@
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.stream.*;
import java.util.concurrent.*;
class ShowThread implements Runnable {
@ -19,8 +20,9 @@ public class WorkStealingPool {
Runtime.getRuntime().availableProcessors());
ExecutorService exec =
Executors.newWorkStealingPool();
for(int i = 0; i < 10; i++)
exec.execute(new ShowThread());
IntStream.range(0, 10)
.mapToObj(n -> new ShowThread())
.forEach(exec::execute);
exec.awaitTermination(1, TimeUnit.SECONDS);
}
}

View File

@ -4,16 +4,14 @@
// Visit http://OnJava8.com for more book information.
// Terminate a program after n seconds
package onjava;
import java.util.*;
import java.util.concurrent.*;
public class TimedAbort {
public TimedAbort(int n) {
new java.util.Timer().schedule(new TimerTask() {
@Override
public void run() {
CompletableFuture.runAsync(() -> {
new Nap(1000 * n);
System.out.println("TimedAbort " + n);
System.exit(0);
}
}, n * 1000);
});
}
}