Question: import java.util.concurrent.*; public class Problem3 { private Integer sum = new Integer(0); public static void main(String[] args) { Problem_3 test = new Problem_3(); System.out.println(What is
import java.util.concurrent.*;
public class Problem3 {
private Integer sum = new Integer(0);
public static void main(String[] args) {
Problem_3 test = new Problem_3();
System.out.println("What is sum ? " + test.sum);
}
public Problem_3() {
ExecutorService executor = Executors.newFixedThreadPool(100);
Task task = new Task();
for (int i = 0; i < 15; i++) {
executor.execute(task);
}
executor.shutdown();
while(!executor.isTerminated()) {
}
}
class Task implements Runnable {
public void run() {
sum++;
}//end run
}//end Task
}//end Problem_3
Running this code does not always give the expected result of 15. Why not, and what change can be made to the code so that it will perform as expected
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
