OnJava8-Examples/lowlevel/SynchronizedEvenSupplier.java

24 lines
720 B
Java
Raw Normal View History

2016-12-07 10:34:41 -08:00
// lowlevel/SynchronizedEvenSupplier.java
2016-07-05 14:46:09 -06:00
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-07-05 14:46:09 -06:00
// 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());
}
}