OnJava8-Examples/concurrency/AtomicIntegerTest.java

32 lines
869 B
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: concurrency/AtomicIntegerTest.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.*;
2015-05-31 18:34:12 -07:00
import net.mindview.util.*;
2015-04-20 15:36:01 -07:00
public class AtomicIntegerTest implements Runnable {
private AtomicInteger i = new AtomicInteger(0);
public int getValue() { return i.get(); }
private void evenIncrement() { i.addAndGet(2); }
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void run() {
while(true)
evenIncrement();
}
public static void main(String[] args) {
2015-05-31 18:34:12 -07:00
new TimedAbort(5); // Terminate after 5 seconds
2015-05-30 19:07:15 -07:00
ExecutorService exec =
Executors.newCachedThreadPool();
2015-04-20 15:36:01 -07:00
AtomicIntegerTest ait = new AtomicIntegerTest();
exec.execute(ait);
while(true) {
int val = ait.getValue();
if(val % 2 != 0) {
System.out.println(val);
System.exit(0);
}
}
}
} ///:~