Assuming 100 milliseconds is enough time for the tasks submitted to the thread executor to complete,
what is the result of executing the following program? (Choose all that apply.)
import java.util.concurrent.*; public class Main { private static AtomicInteger sheepCount1 = new AtomicInteger(0); // w1 private static int sheepCount2 = 0; public static void main(String[] args) throws InterruptedException { ExecutorService service = null; try { /*w w w . ja v a 2 s . c o m*/ service = Executors.newSingleThreadExecutor(); // w2 for(int i=0; i<100; i++) service.execute(() -> {sheepCount1.getAndIncrement(); sheepCount2++;}); // w3 Thread.sleep(100); System.out.println(sheepCount1+" "+sheepCount2); } finally { if(service != null) service.shutdown(); } } }
B.
The code compiles and runs without issue, so D, E, F, and G are incorrect.
The key aspect to notice in the code is that a single-thread executor is used, meaning that no task will be executed concurrently.
Therefore, the results are valid and predictable with 100 100 being the output, and B is the correct answer.
If a pooled thread executor was used with at least two threads, then the sheepCount2++
operations could overwrite each other, making the second value indeterminate at the end of the program.
In this case, C would be the correct answer.