A. correct
Once 2 threads are waiting at the CyclicBarrier, all threads are released and can continue running. It happens two times. So, last 5th thread is waiting forever.
conditions to go through :
The last thread arrives; or
Some other thread interrupts the current thread; or
Some other thread interrupts one of the other waiting threads; or
Some other thread times out while waiting for barrier; or
Some other thread invokes reset() on this barrier.
If any thread is interrupted while waiting, then all other waiting threads will throw BrokenBarrierException and the barrier is placed in the broken state.
B. wrong
it called two times
C. wrong
it never finishes
D. is not correct
Threads in executorService execute for each threads (5).
D. failed
The barrier is called cyclic because it can be re-used after the waiting threads are released.
The number of executed threads will be a multiple of two. it will be 4 out of 5 threads.
A. correct
Once 2 threads are waiting at the CyclicBarrier, all threads are released and can continue running. It happens two times. So, last 5th thread is waiting forever.
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.
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.
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.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
ASPushkin
3 weeks, 3 days agoASPushkin
4 months, 1 week agod7bb0b2
1 year agod7bb0b2
1 year agoduydn
1 year, 2 months agoRoxyFoxy
1 year, 2 months agoeldonivan
1 year, 4 months agoMatt_G
1 year, 6 months agoStavok
1 year, 7 months agoMukes877
1 year, 7 months agoSuperDabicho
1 year, 8 months agoLondeka
1 year, 11 months agoTADIEWA
2 years ago