Skip to content

Commit d8658f0

Browse files
committed
Final pass-through edits
1 parent b8552ee commit d8658f0

File tree

7 files changed

+61
-17
lines changed

7 files changed

+61
-17
lines changed

concurrent/DiningPhilosophers.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Hidden deadlock
66
import java.util.*;
77
import java.util.concurrent.*;
8-
import static java.util.concurrent.TimeUnit.*;
8+
import onjava.Nap;
99

1010
public class DiningPhilosophers {
1111
private StickHolder[] sticks;
@@ -17,7 +17,7 @@ public DiningPhilosophers(int n) {
1717
Arrays.setAll(philosophers, i ->
1818
new Philosopher(i,
1919
sticks[i], sticks[(i + 1) % n])); // [1]
20-
// Fix by reversing stick order:
20+
// Fix by reversing stick order for this one:
2121
// philosophers[1] = // [2]
2222
// new Philosopher(0, sticks[0], sticks[1]);
2323
Arrays.stream(philosophers)
@@ -27,11 +27,6 @@ public static void main(String[] args) {
2727
// Returns right away:
2828
new DiningPhilosophers(5); // [4]
2929
// Keeps main() from exiting:
30-
ScheduledExecutorService sched =
31-
Executors.newScheduledThreadPool(1);
32-
sched.schedule( () -> {
33-
System.out.println("Shutdown");
34-
sched.shutdown();
35-
}, 3, SECONDS);
30+
new Nap(3000, "Shutdown");
3631
}
3732
}

concurrent/SharedConstructorArgument.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Safe implements SharedArg {
1717
private static AtomicInteger counter =
1818
new AtomicInteger();
1919
public int get() {
20-
return counter.getAndAdd(1);
20+
return counter.getAndIncrement();
2121
}
2222
}
2323

concurrent/SynchronizedConstructor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
class SyncConstructor implements HasID {
88
private final int id;
9-
private static Object constructorLock = new Object();
9+
private static Object
10+
constructorLock = new Object();
1011
public SyncConstructor(SharedArg sa) {
1112
synchronized(constructorLock) {
1213
id = sa.get();
@@ -19,7 +20,8 @@ public SyncConstructor(SharedArg sa) {
1920
public class SynchronizedConstructor {
2021
public static void main(String[] args) {
2122
Unsafe unsafe = new Unsafe();
22-
IDChecker.test(() -> new SyncConstructor(unsafe));
23+
IDChecker.test(() ->
24+
new SyncConstructor(unsafe));
2325
}
2426
}
2527
/* Output:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// concurrent/SynchronizedFactory.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.*;
6+
7+
class SyncFactory implements HasID {
8+
private final int id;
9+
private SyncFactory(SharedArg sa) {
10+
id = sa.get();
11+
}
12+
@Override
13+
public int getID() { return id; }
14+
public static synchronized
15+
SyncFactory factory(SharedArg sa) {
16+
return new SyncFactory(sa);
17+
}
18+
}
19+
20+
public class SynchronizedFactory {
21+
public static void main(String[] args) {
22+
Unsafe unsafe = new Unsafe();
23+
IDChecker.test(() ->
24+
SyncFactory.factory(unsafe));
25+
}
26+
}
27+
/* Output:
28+
0
29+
*/

lowlevel/ReOrdering.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// lowlevel/ReOrdering.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+
6+
public class ReOrdering implements Runnable {
7+
int one, two, three, four, five, six;
8+
volatile int volaTile;
9+
@Override
10+
public void run() {
11+
one = 1;
12+
two = 2;
13+
three = 3;
14+
volaTile = 92;
15+
int x = four;
16+
int y = five;
17+
int z = six;
18+
}
19+
}

lowlevel/SettingDefaultHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ public class SettingDefaultHandler {
88
public static void main(String[] args) {
99
Thread.setDefaultUncaughtExceptionHandler(
1010
new MyUncaughtExceptionHandler());
11-
ExecutorService es = Executors.newCachedThreadPool();
11+
ExecutorService es =
12+
Executors.newCachedThreadPool();
1213
es.execute(new ExceptionThread());
1314
es.shutdown();
1415
}

lowlevel/SynchronizedComparison.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ class Caller implements Runnable {
4343
new AtomicLong();
4444
private AtomicBoolean stop =
4545
new AtomicBoolean(false);
46-
class Stop extends TimerTask {
47-
@Override
48-
public void run() { stop.set(true); }
49-
}
5046
@Override
5147
public void run() {
52-
new Timer().schedule(new Stop(), 2500);
48+
new Timer().schedule(new TimerTask() {
49+
public void run() { stop.set(true); }
50+
}, 2500);
5351
while(!stop.get()) {
5452
g.method();
5553
successfulCalls.getAndIncrement();

0 commit comments

Comments
 (0)