Skip to content
Advertisement

In java, how to wait on multiple `Conditions` until any one of them is signaled

Suppose an elevator simulation program, visitors about to take a ride are to wait until any one of the elevator doors opens. i.e. I want to wait on multiple Conditions until any one of them is signaled.

Actually, it doesn’t have to be Conditions, other approaches that can fulfill my need is welcome.

How can this be done in Java?

Advertisement

Answer

You might find CountDownLatch does the job you need. You would instantiate the latch with a count of 1:

CountDownLatch latch = new CountDownLatch(1);

and then share it between your threads. All the threads that wait for the doors to open will do latch.await(). This method will not return until another thread calls latch.countDown().

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement