When running the following code, the 2 start threads will be locked by the CyclicBarrier
*object and waiting for the third thread infinitely to be unlocked
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class MainDeadlock {
public static void main(String[] args) throws InterruptedException {
final CyclicBarrier c = new CyclicBarrier(3);
Runnable r = () -> {
try {
c.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("Run!");
};
new Thread(r).start();
new Thread(r).start();
}
}
So the 2 started thread are waiting for the third third to resolve this barrier. However, according to the Java API documentation of CyclicBarrier, CyclicBarrier
is
A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point
I am confused on how they "wait for each other",
Questions: does "wait for each other" imply a circular wait? If so, how? Strictly speaking, is this a deadlock situation?