OnJava8-Examples/concurrency/MutexEvenSupplier.java
2015-11-15 15:51:35 -08:00

29 lines
812 B
Java

// concurrency/MutexEvenSupplier.java
// ©2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// 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) */