OnJava8-Examples/concurrency/MutexEvenSupplier.java
2015-11-03 12:00:44 -08:00

26 lines
634 B
Java

// concurrency/MutexEvenSupplier.java
// Preventing thread collisions with mutexes.
// {TimeOutDuringTesting}
import java.util.concurrent.locks.*;
public class MutexEvenSupplier extends IntSupplier {
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 MutexEvenSupplier());
}
}
/* Output: (None) */