I want to know why utils.concurrent
have such complicated source code.
This is some code I came up for the CountDownLatch
and after testing it I was expecting to find something similar in the source code, but no, it is super complex.
Is there wrong with my implementation?
public class CountDown {
private int count;
private Object lock;
public CountDown(int count)
{
lock = new Object();
this.count = count;
}
//Just waits until it is notified by CountDown. Keeps waiting if not 0.
public void await() throws InterruptedException
{
synchronized (lock) {
while(count != 0)
{
lock.wait();
}
}
}
//Decreases the count and notifies for await's lock.
public void countDown()
{
synchronized (lock) {
this.count--;
lock.notify();
}
}
}
And here's the source code: Source Code CountDownLatch