2016-07-05 14:46:09 -06:00
|
|
|
// threads/SynchronizedEvenSupplier.java
|
|
|
|
// (c)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.
|
|
|
|
// Simplifying mutexes with the synchronized keyword
|
|
|
|
// {IgnoreOutput} // No output validation
|
2016-07-07 15:12:55 -06:00
|
|
|
import onjava.TimedAbort;
|
2016-07-05 14:46:09 -06:00
|
|
|
|
|
|
|
public class
|
|
|
|
SynchronizedEvenSupplier extends IntSupplier {
|
|
|
|
private int currentEvenValue = 0;
|
|
|
|
@Override
|
|
|
|
public synchronized int next() {
|
|
|
|
++currentEvenValue;
|
|
|
|
Thread.yield(); // Cause failure faster
|
|
|
|
++currentEvenValue;
|
|
|
|
return currentEvenValue;
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
2016-07-07 15:12:55 -06:00
|
|
|
new TimedAbort(4);
|
2016-07-05 14:46:09 -06:00
|
|
|
EvenChecker.test(new SynchronizedEvenSupplier());
|
|
|
|
}
|
|
|
|
}
|