Choice:
A. It outputs Pass! once.
B. It outputs Pass! twice.
C. It outputs Pass! three times.
D. The code will not compile because of line 7.
E. The code will not compile because of line 13.
F. The code will not compile because of line 15.
G. The code compiles but throws an exception at runtime.
H. It compiles but waits forever at runtime.
Explain: new CyclicBarrier(3, ()-> System.out.println("Pass!")); created aCyclicBarrier with limit of 3 and barrier action as printing "Pass!".
IntStream.iterate(1, i->1) generates a stream of 1, .limit(7) gets the first 7 elements from the stream, .forEach() submits a new thread to ExecutorService for each one of the stream elements, for each thread, ()->await(cb) represents the task submitted, which calls CyclicBarrier's await() method.
CyclicBarrier has a limit 3, the executor's thread pool has size 3, the total thread number submitted to ExecutorService is 7. The first 3 threads in thread pool calls await() 3 times, thus breaks the CyclicBarrier, gets "Pass!" printed once; the next 3 threads in thread pool calls await() 3 times, thus breaks the CyclicBarrier, gets "Pass!" printed twice; the 7th thread is submitted into the thread pool and calls await(), since the second and third await() has never been called, the barrier is never broken and the program hangs.
service.shutdown(); does not actually stop any tasks that have already been submitted to the thread executor. Since your thread submitting is done through the stream forEach, even though the main thread exits firstly, all the 7 Callables are still submitted to the ExecutorService (3 in thread pool, the other 4 in ScheduledThreadPoolExecutor's workQueue).