Since the service is single threaded, the monkey1 loop series will always get executed before monkey2, so we can expect monkey1 will always be greater than monkey2, can't we?
import java.util.concurrent.*;
public class MonkeyCounter {
private static AtomicInteger monkey1 = new AtomicInteger(0);
private static AtomicLong monkey2 = new AtomicLong(0);
public static void main(String[] args) {
ExecutorService service = null;
try {
service = Executors.newSingleThreadExecutor();
for(int i=0; i<100; i++)
service.submit(() -> monkey1.getAndIncrement());
for(int i=0; i<100; i++)
service.submit(() -> monkey2.incrementAndGet());
System.out.println(monkey1+" "+monkey2);
} finally {
if(service != null) service.shutdown();
}
}
}