The following problem is on a mock exam for the OCP Java SE 7 Programmer II Exam. The solution says the answer is 0, but my colleagues and I are not sure that the answer isn't between -5 and 5 (which is a choice) Could someone clarify this for us? Here is the code:
import java.util.concurrent.atomic.AtomicInteger;
class AtomicVariableTest {
private static AtomicInteger counter = new AtomicInteger(0);
static class Decrementer extends Thread {
public void run() {
counter.decrementAndGet(); // #1
}
}
static class Incrementer extends Thread {
public void run() {
counter.incrementAndGet(); // #2
}
}
public static void main(String []args) {
for(int i = 0; i < 5; i++) {
new Incrementer().start();
new Decrementer().start();
}
System.out.println(counter);
}
}
Thanks!