given this pice of code
Code: Select all
public class Main {
private AtomicInteger x = new AtomicInteger();
public static void main(String[] args) throws InterruptedException, ExecutionException {
Main p = new Main();
ExecutorService executor = Executors.newFixedThreadPool(2);
Callable<Integer> c = () -> p.x.accumulateAndGet(3, (a, b) -> a + b);
executor.submit(c).get();
executor.submit(c).get();
System.out.println(p.x.get());
executor.shutdown();
}
}
but writing itin this way
Code: Select all
public class Main {
private AtomicInteger x = new AtomicInteger();
public static void main(String[] args) throws InterruptedException, ExecutionException {
Main p = new Main();
ExecutorService executor = Executors.newFixedThreadPool(2);
Callable<Integer> c = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return p.x.accumulateAndGet(3, (a, b) -> a + b);
}
};
executor.submit(c).get();
executor.submit(c).get();
System.out.println(p.x.get());
executor.shutdown();
}
}
shouldn't it not be the same? or am i missing something?