package com.interview.basics.java.cocurrency;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created with IntelliJ IDEA.
* User: stefanie
* Date: 12/24/14
* Time: 2:08 PM
*/
public class AtomicVariable implements Runnable {
private AtomicInteger i = new AtomicInteger(0);
public int getValue() {
return i.get();
}
public void evenIncrement() {
i.addAndGet(2);
}
@Override
public void run() {
while (true) {
evenIncrement();
}
}
public static void main(String[] args) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
System.err.print("Aborting");
System.exit(0);
}
}, 5000);
ExecutorService exec = Executors.newCachedThreadPool();
AtomicVariable sample = new AtomicVariable();
exec.execute(sample);
while (true) {
int val = sample.getValue();
if (val % 2 != 0) {
System.out.println(val);
System.exit(0);
}
}
}
}