1/18
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is CyclicBarrier in Java?
A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.
Which package includes CyclicBarrier?
java.util.concurrent
What is the primary use of CyclicBarrier?
To coordinate a fixed number of threads to wait until all have reached a common barrier point before proceeding.
How is CyclicBarrier different from CountDownLatch?
CyclicBarrier is reusable and used to make threads wait for each other, while CountDownLatch is not reusable and used to make one or more threads wait for others to finish.
How do you initialize a CyclicBarrier?
By specifying the number of parties (threads) and optionally a barrier action: new CyclicBarrier(int parties, Runnable barrierAction)
What is the await()
method in CyclicBarrier?
It causes the calling thread to wait until all threads have called await()
on the barrier.
What happens when the last thread calls await()
on a CyclicBarrier?
All waiting threads are released and the optional barrier action (if provided) is executed.
Is CyclicBarrier reusable?
Yes, it automatically resets after all threads reach the barrier.
What exception can await()
throw if a thread is interrupted?
InterruptedException
What exception is thrown if the barrier is broken while waiting?
BrokenBarrierException
What method is used to check if the barrier is broken?
isBroken()
What method returns the number of parties required to trip the barrier?
getParties()
What method returns the number of threads currently waiting?
getNumberWaiting()
What is a common use case for CyclicBarrier?
Simulating phased tasks like multi-step simulations or parallel computations with synchronization points.
What is the effect of calling reset()
on a CyclicBarrier?
It resets the barrier to its initial state, breaking any currently waiting threads.
What is a barrier action in CyclicBarrier?
An optional Runnable executed once when the last thread reaches the barrier.
What happens if a thread times out or gets interrupted while waiting?
The barrier is broken and all other waiting threads receive a BrokenBarrierException
.
Can CyclicBarrier be used to synchronize fewer threads than the number specified at construction?
No, it always waits for the number of threads (parties) given during initialization.
How does CyclicBarrier improve coordination in parallel processing?
It allows batches of threads to synchronize at multiple stages during execution.