Skip to content

Commit bc786ef

Browse files
committed
Some cleanups during rewrite
1 parent 8833d2e commit bc786ef

6 files changed

Lines changed: 8 additions & 87 deletions

lowlevel/AttemptLocking.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// to give up on trying to acquire a lock
77
import java.util.concurrent.*;
88
import java.util.concurrent.locks.*;
9+
import onjava.Nap;
910

1011
public class AttemptLocking {
1112
private ReentrantLock lock = new ReentrantLock();
@@ -46,7 +47,7 @@ public void run() {
4647
System.out.println("acquired");
4748
}
4849
}.start();
49-
Thread.yield(); // Give the 2nd task a chance
50+
new Nap(10); // Give the 2nd task a chance
5051
al.untimed(); // False -- lock grabbed by task
5152
al.timed(); // False -- lock grabbed by task
5253
}

lowlevel/CriticalSection.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
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-
// (Behavior may have changed in Java 8)
65
// Synchronizing blocks instead of entire methods. Also
76
// demonstrates protection of a non-thread-safe class
87
// with a thread-safe one.
9-
// {java lowlevel.CriticalSection}
10-
package lowlevel;
118
import java.util.concurrent.*;
129
import java.util.concurrent.atomic.*;
1310
import java.util.*;

lowlevel/ExplicitCriticalSection.java

Lines changed: 0 additions & 79 deletions
This file was deleted.

lowlevel/MutexEvenProducer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Preventing thread collisions with mutexes
66
// {IgnoreOutput} // No output validation
77
import java.util.concurrent.locks.*;
8+
import onjava.Nap;
89

910
public class MutexEvenProducer extends IntGenerator {
1011
private int currentEvenValue = 0;
@@ -14,7 +15,7 @@ public int next() {
1415
lock.lock();
1516
try {
1617
++currentEvenValue;
17-
Thread.yield(); // Cause failure faster
18+
new Nap(10); // Cause failure faster
1819
++currentEvenValue;
1920
return currentEvenValue;
2021
} finally {

lowlevel/PriorityBlockingQueueDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void run() {
7373
// Fill it up fast with random priorities:
7474
for(int i = 0; i < 20; i++) {
7575
queue.add(new PrioritizedTask(rand.nextInt(10)));
76-
Thread.yield();
76+
new Nap(10);
7777
}
7878
// Trickle in highest-priority jobs:
7979
for(int i = 0; i < 10; i++) {

lowlevel/SyncObject.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
// Synchronizing on another object
6+
import onjava.Nap;
67

78
class DualSynch {
89
private Object syncObject = new Object();
910
public synchronized void f() {
1011
for(int i = 0; i < 5; i++) {
1112
System.out.println("f()");
12-
Thread.yield();
13+
new Nap(10);
1314
}
1415
}
1516
public void g() {
1617
synchronized(syncObject) {
1718
for(int i = 0; i < 5; i++) {
1819
System.out.println("g()");
19-
Thread.yield();
20+
new Nap(10);
2021
}
2122
}
2223
}

0 commit comments

Comments
 (0)