49 lines
1.1 KiB
Java
49 lines
1.1 KiB
Java
|
//: concurrency/ExplicitCriticalSection.java
|
||
|
// Using explicit Lock objects to create critical sections.
|
||
|
package concurrency;
|
||
|
import java.util.concurrent.locks.*;
|
||
|
|
||
|
// Synchronize the entire method:
|
||
|
class ExplicitPairManager1 extends PairManager {
|
||
|
private Lock lock = new ReentrantLock();
|
||
|
public synchronized 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();
|
||
|
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: (Sample)
|
||
|
pm1: Pair: x: 15, y: 15 checkCounter = 174035
|
||
|
pm2: Pair: x: 16, y: 16 checkCounter = 2608588
|
||
|
*///:~
|