24 lines
601 B
Java
24 lines
601 B
Java
|
//: concurrency/MutexEvenGenerator.java
|
||
|
// Preventing thread collisions with mutexes.
|
||
|
// {RunByHand}
|
||
|
import java.util.concurrent.locks.*;
|
||
|
|
||
|
public class MutexEvenGenerator extends IntGenerator {
|
||
|
private int currentEvenValue = 0;
|
||
|
private Lock lock = new ReentrantLock();
|
||
|
public int next() {
|
||
|
lock.lock();
|
||
|
try {
|
||
|
++currentEvenValue;
|
||
|
Thread.yield(); // Cause failure faster
|
||
|
++currentEvenValue;
|
||
|
return currentEvenValue;
|
||
|
} finally {
|
||
|
lock.unlock();
|
||
|
}
|
||
|
}
|
||
|
public static void main(String[] args) {
|
||
|
EvenChecker.test(new MutexEvenGenerator());
|
||
|
}
|
||
|
} ///:~
|