OnJava8-Examples/concurrency/MutexEvenGenerator.java
2015-09-07 11:44:36 -06:00

27 lines
679 B
Java

// concurrency/MutexEvenGenerator.java
// ©2015 MindView LLC: see Copyright.txt
// Preventing thread collisions with mutexes.
// {TimeOutDuringTesting}
import java.util.concurrent.locks.*;
public class MutexEvenGenerator extends IntGenerator {
private int currentEvenValue = 0;
private Lock lock = new ReentrantLock();
@Override
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());
}
}
/* Output: (None) */