Some cleanups during rewrite
This commit is contained in:
parent
8833d2e228
commit
bc786ef438
@ -6,6 +6,7 @@
|
|||||||
// to give up on trying to acquire a lock
|
// to give up on trying to acquire a lock
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.locks.*;
|
import java.util.concurrent.locks.*;
|
||||||
|
import onjava.Nap;
|
||||||
|
|
||||||
public class AttemptLocking {
|
public class AttemptLocking {
|
||||||
private ReentrantLock lock = new ReentrantLock();
|
private ReentrantLock lock = new ReentrantLock();
|
||||||
@ -46,7 +47,7 @@ public class AttemptLocking {
|
|||||||
System.out.println("acquired");
|
System.out.println("acquired");
|
||||||
}
|
}
|
||||||
}.start();
|
}.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.untimed(); // False -- lock grabbed by task
|
||||||
al.timed(); // False -- lock grabbed by task
|
al.timed(); // False -- lock grabbed by task
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,9 @@
|
|||||||
// (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.
|
||||||
// (Behavior may have changed in Java 8)
|
|
||||||
// Synchronizing blocks instead of entire methods. Also
|
// Synchronizing blocks instead of entire methods. Also
|
||||||
// demonstrates protection of a non-thread-safe class
|
// demonstrates protection of a non-thread-safe class
|
||||||
// with a thread-safe one.
|
// with a thread-safe one.
|
||||||
// {java lowlevel.CriticalSection}
|
|
||||||
package lowlevel;
|
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.*;
|
import java.util.concurrent.atomic.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
@ -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)
|
|
||||||
*/
|
|
@ -5,6 +5,7 @@
|
|||||||
// 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.Nap;
|
||||||
|
|
||||||
public class MutexEvenProducer extends IntGenerator {
|
public class MutexEvenProducer extends IntGenerator {
|
||||||
private int currentEvenValue = 0;
|
private int currentEvenValue = 0;
|
||||||
@ -14,7 +15,7 @@ public class MutexEvenProducer extends IntGenerator {
|
|||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
++currentEvenValue;
|
++currentEvenValue;
|
||||||
Thread.yield(); // Cause failure faster
|
new Nap(10); // Cause failure faster
|
||||||
++currentEvenValue;
|
++currentEvenValue;
|
||||||
return currentEvenValue;
|
return currentEvenValue;
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -73,7 +73,7 @@ class PrioritizedTaskProducer implements Runnable {
|
|||||||
// Fill it up fast with random priorities:
|
// Fill it up fast with random priorities:
|
||||||
for(int i = 0; i < 20; i++) {
|
for(int i = 0; i < 20; i++) {
|
||||||
queue.add(new PrioritizedTask(rand.nextInt(10)));
|
queue.add(new PrioritizedTask(rand.nextInt(10)));
|
||||||
Thread.yield();
|
new Nap(10);
|
||||||
}
|
}
|
||||||
// Trickle in highest-priority jobs:
|
// Trickle in highest-priority jobs:
|
||||||
for(int i = 0; i < 10; i++) {
|
for(int i = 0; i < 10; i++) {
|
||||||
|
@ -3,20 +3,21 @@
|
|||||||
// 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.
|
||||||
// Synchronizing on another object
|
// Synchronizing on another object
|
||||||
|
import onjava.Nap;
|
||||||
|
|
||||||
class DualSynch {
|
class DualSynch {
|
||||||
private Object syncObject = new Object();
|
private Object syncObject = new Object();
|
||||||
public synchronized void f() {
|
public synchronized void f() {
|
||||||
for(int i = 0; i < 5; i++) {
|
for(int i = 0; i < 5; i++) {
|
||||||
System.out.println("f()");
|
System.out.println("f()");
|
||||||
Thread.yield();
|
new Nap(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void g() {
|
public void g() {
|
||||||
synchronized(syncObject) {
|
synchronized(syncObject) {
|
||||||
for(int i = 0; i < 5; i++) {
|
for(int i = 0; i < 5; i++) {
|
||||||
System.out.println("g()");
|
System.out.println("g()");
|
||||||
Thread.yield();
|
new Nap(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user