Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class Latch
- {
- private final Object monitor = new Object();
- private int counter = 0;
- private boolean canStart = true;
- public boolean tryStart()
- {
- synchronized (monitor)
- {
- if (canStart)
- {
- counter++;
- return true;
- }
- return false;
- }
- }
- public void finish()
- {
- synchronized (monitor)
- {
- counter--;
- if (0 == counter)
- {
- monitor.notifyAll();
- }
- }
- }
- public void waitForZero() throws InterruptedException
- {
- synchronized (monitor)
- {
- canStart = false;
- while (0 != counter)
- {
- monitor.wait();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement