Some cleanups during rewrite

This commit is contained in:
Bruce Eckel 2017-01-12 11:15:03 -08:00
parent 8833d2e228
commit bc786ef438
6 changed files with 8 additions and 87 deletions

View File

@ -6,6 +6,7 @@
// to give up on trying to acquire a lock
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
import onjava.Nap;
public class AttemptLocking {
private ReentrantLock lock = new ReentrantLock();
@ -46,7 +47,7 @@ public class AttemptLocking {
System.out.println("acquired");
}
}.start();
Thread.yield(); // Give the 2nd task a chance
new Nap(10); // Give the 2nd task a chance
al.untimed(); // False -- lock grabbed by task
al.timed(); // False -- lock grabbed by task
}

View File

@ -2,12 +2,9 @@
// (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.
// (Behavior may have changed in Java 8)
// Synchronizing blocks instead of entire methods. Also
// demonstrates protection of a non-thread-safe class
// with a thread-safe one.
// {java lowlevel.CriticalSection}
package lowlevel;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.*;

View File

@ -1,79 +0,0 @@
// lowlevel/ExplicitCriticalSection.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.
// {ThrowsException} on a multiprocessor machine
// Using explicit Lock objects to create critical sections
// {java lowlevel.ExplicitCriticalSection}
package lowlevel;
import java.util.concurrent.locks.*;
// Synchronize the entire method:
class ExplicitPairManager1 extends PairManager {
private Lock lock = new ReentrantLock();
@Override
public void increment() {
lock.lock();
try {
p.incrementX();
p.incrementY();
store(getPair());
} finally {
lock.unlock();
}
}
}
// Use a critical section:
class ExplicitPairManager2 extends PairManager {
private Lock lock = new ReentrantLock();
@Override
public void increment() {
Pair temp;
lock.lock();
try {
p.incrementX();
p.incrementY();
temp = getPair();
} finally {
lock.unlock();
}
store(temp);
}
}
public class ExplicitCriticalSection {
public static void
main(String[] args) throws Exception {
PairManager
pman1 = new ExplicitPairManager1(),
pman2 = new ExplicitPairManager2();
CriticalSection.testApproaches(pman1, pman2);
}
}
/* Output:
pm1: Pair: x: 10, y: 10 checkCounter = 1713716
pm2: Pair: x: 10, y: 10 checkCounter = 1717747
___[ Error Output ]___
Exception in thread "pool-1-thread-4" Exception in thread
"pool-1-thread-3" threads.Pair$PairValuesNotEqualException:
Pair values not equal: x: 2, y: 1
at threads.Pair.checkState(CriticalSection.java:37)
at
threads.PairChecker.run(CriticalSection.java:111)
at java.util.concurrent.ThreadPoolExecutor.runWorke
r(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.r
un(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
threads.Pair$PairValuesNotEqualException: Pair values not
equal: x: 2, y: 1
at threads.Pair.checkState(CriticalSection.java:37)
at
threads.PairChecker.run(CriticalSection.java:111)
at java.util.concurrent.ThreadPoolExecutor.runWorke
r(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.r
un(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
*/

View File

@ -5,6 +5,7 @@
// Preventing thread collisions with mutexes
// {IgnoreOutput} // No output validation
import java.util.concurrent.locks.*;
import onjava.Nap;
public class MutexEvenProducer extends IntGenerator {
private int currentEvenValue = 0;
@ -14,7 +15,7 @@ public class MutexEvenProducer extends IntGenerator {
lock.lock();
try {
++currentEvenValue;
Thread.yield(); // Cause failure faster
new Nap(10); // Cause failure faster
++currentEvenValue;
return currentEvenValue;
} finally {

View File

@ -73,7 +73,7 @@ class PrioritizedTaskProducer implements Runnable {
// Fill it up fast with random priorities:
for(int i = 0; i < 20; i++) {
queue.add(new PrioritizedTask(rand.nextInt(10)));
Thread.yield();
new Nap(10);
}
// Trickle in highest-priority jobs:
for(int i = 0; i < 10; i++) {

View File

@ -3,20 +3,21 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Synchronizing on another object
import onjava.Nap;
class DualSynch {
private Object syncObject = new Object();
public synchronized void f() {
for(int i = 0; i < 5; i++) {
System.out.println("f()");
Thread.yield();
new Nap(10);
}
}
public void g() {
synchronized(syncObject) {
for(int i = 0; i < 5; i++) {
System.out.println("g()");
Thread.yield();
new Nap(10);
}
}
}