Final pass-through edits
This commit is contained in:
parent
b8552ee24c
commit
d8658f0a52
@ -5,7 +5,7 @@
|
||||
// Hidden deadlock
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import static java.util.concurrent.TimeUnit.*;
|
||||
import onjava.Nap;
|
||||
|
||||
public class DiningPhilosophers {
|
||||
private StickHolder[] sticks;
|
||||
@ -17,7 +17,7 @@ public class DiningPhilosophers {
|
||||
Arrays.setAll(philosophers, i ->
|
||||
new Philosopher(i,
|
||||
sticks[i], sticks[(i + 1) % n])); // [1]
|
||||
// Fix by reversing stick order:
|
||||
// Fix by reversing stick order for this one:
|
||||
// philosophers[1] = // [2]
|
||||
// new Philosopher(0, sticks[0], sticks[1]);
|
||||
Arrays.stream(philosophers)
|
||||
@ -27,11 +27,6 @@ public class DiningPhilosophers {
|
||||
// Returns right away:
|
||||
new DiningPhilosophers(5); // [4]
|
||||
// Keeps main() from exiting:
|
||||
ScheduledExecutorService sched =
|
||||
Executors.newScheduledThreadPool(1);
|
||||
sched.schedule( () -> {
|
||||
System.out.println("Shutdown");
|
||||
sched.shutdown();
|
||||
}, 3, SECONDS);
|
||||
new Nap(3000, "Shutdown");
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ class Safe implements SharedArg {
|
||||
private static AtomicInteger counter =
|
||||
new AtomicInteger();
|
||||
public int get() {
|
||||
return counter.getAndAdd(1);
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,8 @@ import java.util.concurrent.atomic.*;
|
||||
|
||||
class SyncConstructor implements HasID {
|
||||
private final int id;
|
||||
private static Object constructorLock = new Object();
|
||||
private static Object
|
||||
constructorLock = new Object();
|
||||
public SyncConstructor(SharedArg sa) {
|
||||
synchronized(constructorLock) {
|
||||
id = sa.get();
|
||||
@ -19,7 +20,8 @@ class SyncConstructor implements HasID {
|
||||
public class SynchronizedConstructor {
|
||||
public static void main(String[] args) {
|
||||
Unsafe unsafe = new Unsafe();
|
||||
IDChecker.test(() -> new SyncConstructor(unsafe));
|
||||
IDChecker.test(() ->
|
||||
new SyncConstructor(unsafe));
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
|
29
concurrent/SynchronizedFactory.java
Normal file
29
concurrent/SynchronizedFactory.java
Normal file
@ -0,0 +1,29 @@
|
||||
// concurrent/SynchronizedFactory.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.*;
|
||||
|
||||
class SyncFactory implements HasID {
|
||||
private final int id;
|
||||
private SyncFactory(SharedArg sa) {
|
||||
id = sa.get();
|
||||
}
|
||||
@Override
|
||||
public int getID() { return id; }
|
||||
public static synchronized
|
||||
SyncFactory factory(SharedArg sa) {
|
||||
return new SyncFactory(sa);
|
||||
}
|
||||
}
|
||||
|
||||
public class SynchronizedFactory {
|
||||
public static void main(String[] args) {
|
||||
Unsafe unsafe = new Unsafe();
|
||||
IDChecker.test(() ->
|
||||
SyncFactory.factory(unsafe));
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
0
|
||||
*/
|
19
lowlevel/ReOrdering.java
Normal file
19
lowlevel/ReOrdering.java
Normal file
@ -0,0 +1,19 @@
|
||||
// lowlevel/ReOrdering.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 class ReOrdering implements Runnable {
|
||||
int one, two, three, four, five, six;
|
||||
volatile int volaTile;
|
||||
@Override
|
||||
public void run() {
|
||||
one = 1;
|
||||
two = 2;
|
||||
three = 3;
|
||||
volaTile = 92;
|
||||
int x = four;
|
||||
int y = five;
|
||||
int z = six;
|
||||
}
|
||||
}
|
@ -8,7 +8,8 @@ public class SettingDefaultHandler {
|
||||
public static void main(String[] args) {
|
||||
Thread.setDefaultUncaughtExceptionHandler(
|
||||
new MyUncaughtExceptionHandler());
|
||||
ExecutorService es = Executors.newCachedThreadPool();
|
||||
ExecutorService es =
|
||||
Executors.newCachedThreadPool();
|
||||
es.execute(new ExceptionThread());
|
||||
es.shutdown();
|
||||
}
|
||||
|
@ -43,13 +43,11 @@ class Caller implements Runnable {
|
||||
new AtomicLong();
|
||||
private AtomicBoolean stop =
|
||||
new AtomicBoolean(false);
|
||||
class Stop extends TimerTask {
|
||||
@Override
|
||||
public void run() { stop.set(true); }
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
new Timer().schedule(new Stop(), 2500);
|
||||
new Timer().schedule(new TimerTask() {
|
||||
public void run() { stop.set(true); }
|
||||
}, 2500);
|
||||
while(!stop.get()) {
|
||||
g.method();
|
||||
successfulCalls.getAndIncrement();
|
||||
|
Loading…
x
Reference in New Issue
Block a user