Name changes and new TimedAbort
This commit is contained in:
parent
d3ca481bdb
commit
8833d2e228
@ -1,13 +1,12 @@
|
|||||||
// lowlevel/AtomicEvenSupplier.java
|
// lowlevel/AtomicEvenProducer.java
|
||||||
// (c)2017 MindView LLC: see Copyright.txt
|
// (c)2017 MindView LLC: see Copyright.txt
|
||||||
// We make no guarantees that this code is fit for any purpose.
|
// We make no guarantees that this code is fit for any purpose.
|
||||||
// Visit http://OnJava8.com for more book information.
|
// Visit http://OnJava8.com for more book information.
|
||||||
// Atomic classes are occasionally useful in regular code
|
// Atomic classes are occasionally useful in regular code
|
||||||
// {IgnoreOutput} // No output validation
|
// {IgnoreOutput} // No output validation
|
||||||
import java.util.concurrent.atomic.*;
|
import java.util.concurrent.atomic.*;
|
||||||
import onjava.TimedAbort;
|
|
||||||
|
|
||||||
public class AtomicEvenSupplier extends IntSupplier {
|
public class AtomicEvenProducer extends IntGenerator {
|
||||||
private AtomicInteger currentEvenValue =
|
private AtomicInteger currentEvenValue =
|
||||||
new AtomicInteger(0);
|
new AtomicInteger(0);
|
||||||
@Override
|
@Override
|
||||||
@ -15,7 +14,6 @@ public class AtomicEvenSupplier extends IntSupplier {
|
|||||||
return currentEvenValue.addAndGet(2);
|
return currentEvenValue.addAndGet(2);
|
||||||
}
|
}
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new TimedAbort(4);
|
EvenChecker.test(new AtomicEvenProducer());
|
||||||
EvenChecker.test(new AtomicEvenSupplier());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,13 +3,14 @@
|
|||||||
// We make no guarantees that this code is fit for any purpose.
|
// We make no guarantees that this code is fit for any purpose.
|
||||||
// Visit http://OnJava8.com for more book information.
|
// Visit http://OnJava8.com for more book information.
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
import onjava.TimedAbort;
|
||||||
|
|
||||||
public class EvenChecker implements Runnable {
|
public class EvenChecker implements Runnable {
|
||||||
private IntSupplier generator;
|
private IntGenerator generator;
|
||||||
private final int id;
|
private final int id;
|
||||||
public EvenChecker(IntSupplier g, int ident) {
|
public EvenChecker(IntGenerator generator, int id) {
|
||||||
generator = g;
|
this.generator = generator;
|
||||||
id = ident;
|
this.id = id;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -21,8 +22,8 @@ public class EvenChecker implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Test any type of IntSupplier:
|
// Test any IntGenerator:
|
||||||
public static void test(IntSupplier gp, int count) {
|
public static void test(IntGenerator gp, int count) {
|
||||||
System.out.println("Press Control-C to exit");
|
System.out.println("Press Control-C to exit");
|
||||||
ExecutorService es = Executors.newCachedThreadPool();
|
ExecutorService es = Executors.newCachedThreadPool();
|
||||||
for(int i = 0; i < count; i++)
|
for(int i = 0; i < count; i++)
|
||||||
@ -30,7 +31,8 @@ public class EvenChecker implements Runnable {
|
|||||||
es.shutdown();
|
es.shutdown();
|
||||||
}
|
}
|
||||||
// Default value for count:
|
// Default value for count:
|
||||||
public static void test(IntSupplier gp) {
|
public static void test(IntGenerator gp) {
|
||||||
|
new TimedAbort(4);
|
||||||
test(gp, 10);
|
test(gp, 10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,25 @@
|
|||||||
// lowlevel/EvenSupplier.java
|
// lowlevel/EvenProducer.java
|
||||||
// (c)2017 MindView LLC: see Copyright.txt
|
// (c)2017 MindView LLC: see Copyright.txt
|
||||||
// We make no guarantees that this code is fit for any purpose.
|
// We make no guarantees that this code is fit for any purpose.
|
||||||
// Visit http://OnJava8.com for more book information.
|
// Visit http://OnJava8.com for more book information.
|
||||||
// When threads collide
|
// When threads collide
|
||||||
import onjava.TimedAbort;
|
|
||||||
|
|
||||||
public class EvenSupplier extends IntSupplier {
|
public class EvenProducer extends IntGenerator {
|
||||||
private int currentEvenValue = 0;
|
private int currentEvenValue = 0;
|
||||||
@Override
|
@Override
|
||||||
public int next() {
|
public int next() {
|
||||||
++currentEvenValue; // Danger point here!
|
++currentEvenValue; // [1]
|
||||||
++currentEvenValue;
|
++currentEvenValue;
|
||||||
return currentEvenValue;
|
return currentEvenValue;
|
||||||
}
|
}
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new TimedAbort(4);
|
EvenChecker.test(new EvenProducer());
|
||||||
EvenChecker.test(new EvenSupplier());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Output:
|
/* Output:
|
||||||
Press Control-C to exit
|
Press Control-C to exit
|
||||||
193 not even!
|
841 not even!
|
||||||
191 not even!
|
847 not even!
|
||||||
TimedAbort 4
|
845 not even!
|
||||||
|
843 not even!
|
||||||
*/
|
*/
|
@ -12,7 +12,8 @@ public class ExceptionThread implements Runnable {
|
|||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ExecutorService es = Executors.newCachedThreadPool();
|
ExecutorService es =
|
||||||
|
Executors.newCachedThreadPool();
|
||||||
es.execute(new ExceptionThread());
|
es.execute(new ExceptionThread());
|
||||||
es.shutdown();
|
es.shutdown();
|
||||||
}
|
}
|
||||||
|
15
lowlevel/IntGenerator.java
Normal file
15
lowlevel/IntGenerator.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -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; }
|
|
||||||
}
|
|
@ -1,13 +1,12 @@
|
|||||||
// lowlevel/MutexEvenSupplier.java
|
// lowlevel/MutexEvenProducer.java
|
||||||
// (c)2017 MindView LLC: see Copyright.txt
|
// (c)2017 MindView LLC: see Copyright.txt
|
||||||
// We make no guarantees that this code is fit for any purpose.
|
// We make no guarantees that this code is fit for any purpose.
|
||||||
// Visit http://OnJava8.com for more book information.
|
// Visit http://OnJava8.com for more book information.
|
||||||
// Preventing thread collisions with mutexes
|
// Preventing thread collisions with mutexes
|
||||||
// {IgnoreOutput} // No output validation
|
// {IgnoreOutput} // No output validation
|
||||||
import java.util.concurrent.locks.*;
|
import java.util.concurrent.locks.*;
|
||||||
import onjava.TimedAbort;
|
|
||||||
|
|
||||||
public class MutexEvenSupplier extends IntSupplier {
|
public class MutexEvenProducer extends IntGenerator {
|
||||||
private int currentEvenValue = 0;
|
private int currentEvenValue = 0;
|
||||||
private Lock lock = new ReentrantLock();
|
private Lock lock = new ReentrantLock();
|
||||||
@Override
|
@Override
|
||||||
@ -23,7 +22,6 @@ public class MutexEvenSupplier extends IntSupplier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new TimedAbort(4);
|
EvenChecker.test(new MutexEvenProducer());
|
||||||
EvenChecker.test(new MutexEvenSupplier());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,23 +1,22 @@
|
|||||||
// lowlevel/SynchronizedEvenSupplier.java
|
// lowlevel/SynchronizedEvenProducer.java
|
||||||
// (c)2017 MindView LLC: see Copyright.txt
|
// (c)2017 MindView LLC: see Copyright.txt
|
||||||
// We make no guarantees that this code is fit for any purpose.
|
// We make no guarantees that this code is fit for any purpose.
|
||||||
// Visit http://OnJava8.com for more book information.
|
// Visit http://OnJava8.com for more book information.
|
||||||
// Simplifying mutexes with the synchronized keyword
|
// Simplifying mutexes with the synchronized keyword
|
||||||
// {IgnoreOutput} // No output validation
|
// {IgnoreOutput} // No output validation
|
||||||
import onjava.TimedAbort;
|
import onjava.Nap;
|
||||||
|
|
||||||
public class
|
public class
|
||||||
SynchronizedEvenSupplier extends IntSupplier {
|
SynchronizedEvenProducer extends IntGenerator {
|
||||||
private int currentEvenValue = 0;
|
private int currentEvenValue = 0;
|
||||||
@Override
|
@Override
|
||||||
public synchronized int next() {
|
public synchronized int next() {
|
||||||
++currentEvenValue;
|
++currentEvenValue;
|
||||||
Thread.yield(); // Cause failure faster
|
new Nap(10); // Cause failure faster
|
||||||
++currentEvenValue;
|
++currentEvenValue;
|
||||||
return currentEvenValue;
|
return currentEvenValue;
|
||||||
}
|
}
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new TimedAbort(4);
|
EvenChecker.test(new SynchronizedEvenProducer());
|
||||||
EvenChecker.test(new SynchronizedEvenSupplier());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
17
lowlevel/TestAbort.java
Normal file
17
lowlevel/TestAbort.java
Normal 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
|
||||||
|
*/
|
@ -2,6 +2,7 @@
|
|||||||
// (c)2017 MindView LLC: see Copyright.txt
|
// (c)2017 MindView LLC: see Copyright.txt
|
||||||
// We make no guarantees that this code is fit for any purpose.
|
// We make no guarantees that this code is fit for any purpose.
|
||||||
// Visit http://OnJava8.com for more book information.
|
// Visit http://OnJava8.com for more book information.
|
||||||
|
import java.util.stream.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
class ShowThread implements Runnable {
|
class ShowThread implements Runnable {
|
||||||
@ -19,8 +20,9 @@ public class WorkStealingPool {
|
|||||||
Runtime.getRuntime().availableProcessors());
|
Runtime.getRuntime().availableProcessors());
|
||||||
ExecutorService exec =
|
ExecutorService exec =
|
||||||
Executors.newWorkStealingPool();
|
Executors.newWorkStealingPool();
|
||||||
for(int i = 0; i < 10; i++)
|
IntStream.range(0, 10)
|
||||||
exec.execute(new ShowThread());
|
.mapToObj(n -> new ShowThread())
|
||||||
|
.forEach(exec::execute);
|
||||||
exec.awaitTermination(1, TimeUnit.SECONDS);
|
exec.awaitTermination(1, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,16 +4,14 @@
|
|||||||
// Visit http://OnJava8.com for more book information.
|
// Visit http://OnJava8.com for more book information.
|
||||||
// Terminate a program after n seconds
|
// Terminate a program after n seconds
|
||||||
package onjava;
|
package onjava;
|
||||||
import java.util.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
public class TimedAbort {
|
public class TimedAbort {
|
||||||
public TimedAbort(int n) {
|
public TimedAbort(int n) {
|
||||||
new java.util.Timer().schedule(new TimerTask() {
|
CompletableFuture.runAsync(() -> {
|
||||||
@Override
|
new Nap(1000 * n);
|
||||||
public void run() {
|
|
||||||
System.out.println("TimedAbort " + n);
|
System.out.println("TimedAbort " + n);
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
});
|
||||||
}, n * 1000);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user