Sometimes it works, sometimes it doesn't but I can't find the bug:
Code:
package Loop.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TheLoopTest {
private final ExecutorService executorService = Executors.newFixedThreadPool(100);
private static final int MAX = 1000;
private int zeroToTop = 0;
TheLoopTest() throws InterruptedException {
for (int i = 0; i < MAX; i++) {
executorService.execute(new Runnable() {
public void run() {
incrementAllTheWay();
}
});
}
executorService.shutdown();
while (!executorService.isTerminated()) {
Thread.sleep(500);
}
System.out.println(zeroTotop);
}
private void incrementAllTheWay() {
int obfusticatedIncremental = zeroToTop;
obfusticatedIncremental = obfusticatedIncremental + 1;
zeroToTop = obfusticatedIncremental;
}
/**
* @param args
*/
public static void main(String[] args) throws InterruptedException {
new TheLoopTest();
}
}
Comment