OnJava8-Examples/lowlevel/SynchronizedEvenProducer.java
2017-01-22 16:48:11 -08:00

25 lines
688 B
Java

// lowlevel/SynchronizedEvenProducer.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Simplifying mutexes with the synchronized keyword
import onjava.Nap;
public class
SynchronizedEvenProducer extends IntGenerator {
private int currentEvenValue = 0;
@Override
public synchronized int next() {
++currentEvenValue;
new Nap(0.01); // Cause failure faster
++currentEvenValue;
return currentEvenValue;
}
public static void main(String[] args) {
EvenChecker.test(new SynchronizedEvenProducer());
}
}
/* Output:
No odd numbers discovered
*/