Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 28

Given the code fragment:

Which statement is true?

    Correct Answer: A

    The provided code fragment creates an `ExecutorService` with a fixed thread pool of 5 threads and a `CyclicBarrier` that requires 2 parties before the barrier's action is executed. The code uses `IntStream.range(0, 5)` to generate 5 tasks. Each task adds its corresponding number to the `CopyOnWriteArrayList` and then waits at the barrier. Given that the barrier requires 2 parties to proceed, one thread will always be left waiting since there are an odd number of threads and iterations (5). As a result, the program will never complete as one thread will perpetually wait at the barrier, preventing the executor service from shutting down. Thus, the correct statement is that the code never finishes.

Discussion
Matt_GOption: A

Running this locally, the code never finishes and the contents of the list is printed out twice (which may be in a random order). As the CyclicBarrier has a party count of two, and an odd number of threads, there will always remain one thread waiting on the barrier. Change to adding 6 numbers and it will finish fine.

TADIEWAOption: A

A is correct

d7bb0b2Option: C

C: finish ok and print the list ANY ORDER [1, 3, 2, 0, 4]

d7bb0b2

wait a moment lol: A is correct :D not fisnish: Explanation: A. It does finish. After all threads reach the barrier, the action of the CyclicBarrier is executed and then the executor service is shut down. B. The action of CyclicBarrier is called only once when all five threads reach the barrier. C. It finishes without any exception. The CyclicBarrier ensures that all threads have reached a common barrier point before proceeding. If any thread is interrupted while waiting, it will throw an InterruptedException, and if the barrier is broken while a thread is waiting, it will throw a BrokenBarrierException. However, these exceptions are caught and handled in the code. D. The ExecutorService executes a task for each of the five threads, not two.

duydnOption: A

public static void main(String[] args) { List<Integer> list = new CopyOnWriteArrayList<>(); ExecutorService executorService = Executors.newFixedThreadPool(5); CyclicBarrier barrier = new CyclicBarrier(2, () -> System.out.println(list)); IntStream.range(0,5).forEach(n -> executorService.execute(() -> { try { list.add(n); barrier.await(); } catch (InterruptedException | BrokenBarrierException e) { System.out.println("err"); } })); executorService.shutdown(); } -> A

RoxyFoxyOption: A

it never finishes.

eldonivanOption: A

If you run this in the compiler, it outputs but check the run, it does not stop. it does not finish

StavokOption: C

It finishes smoothly, TESTED

Mukes877Option: B

The correct statement is: B. The action of CyclicBarrier is called five times. Explanation: The code creates a CyclicBarrier with a party size of 2 and an action that prints the list. It then launches 5 threads using the executorService, each adding an element to the list and waiting at the barrier using barrier.await(). Since the party size is 2, the barrier action will be triggered when 2 threads reach the barrier. As there are 5 threads, this will happen a total of 5/2 = 2 times. Each time the barrier action is called, it will print the current state of the list. Therefore, option B is the correct statement. The action of the CyclicBarrier is called five times.

SuperDabichoOption: C

It finished cool, guys (Option C is correct)

LondekaOption: C

C is correct.