What is the result of executing the following application? (Choose all that apply.)
import java.util.concurrent.*; import java.util.stream.*; public class Main { public static void await(CyclicBarrier cb) { try { //from w ww . j a v a 2 s.c o m cb.await(); } catch (InterruptedException | BrokenBarrierException e) { // Handle exception } } public static void main(String[] args) { final CyclicBarrier cb = new CyclicBarrier(3,()-> System.out. println("Clean!"));// u1 ExecutorService service = Executors.newScheduledThreadPool(2); IntStream.iterate(1, i-> 1) // u2 .limit(12) .forEach(i-> service.submit( // u3 ()-> await(cb))); // u4 service.shutdown(); } }
H.
The code compiles without issue, so C, D, E, and F are incorrect.
The key to understanding this code is to notice that our thread pool size is only 2, but our CyclicBarrier limit is 3.
Even though 12 tasks are all successfully submitted to the thread executor service by way of the stream forEach()
method, the first two tasks will use up both available threads and wait indefinitely.
Since a third await()
is never executed, the barrier is never broken and the program hangs, making H the only correct answer.
Nothing is ever outputted nor is any exception thrown, so A, B, and G are incorrect.