-2

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

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
AFP_555
  • 2,392
  • 4
  • 25
  • 45
  • Your implementation just doesn't have as many features. If you add all the missing features, your implementation will look much the same. – David Schwartz May 01 '16 at 06:14
  • Features such as? There are very complex methods, for example, what's up with that loop inside the private class Sync? – AFP_555 May 01 '16 at 06:17
  • Oh wow... If someone's going to downvote, at least tell me why please. – AFP_555 May 01 '16 at 06:18
  • Features like the feature that that loop implements -- a "try release" operation. – David Schwartz May 01 '16 at 06:25
  • The downvotes are indeed rather harsh (at least, there are far worse questions). However, the usage of `AbstractQueuedSynchronizer` in `CountDownLatch` may not even have been "necessary", but it's a basis for many classes from the `concurrent` package. Further details (e.g. regarding the *shared* and *exclusive* behavior of the synchronizer) may be beyond the scope of an answer here (or at least, if this is what the question was aiming at, then it should be stated differently) – Marco13 May 01 '16 at 12:56

1 Answers1

2

CountDownLatch seems to be just a wrapper to AbstractQueuedSynchronizer. I think it could be the case that Doug noticed that and decided to just go with that approach, which further lead to its current design.

One important feature that I see in CountDownLatch, via the private Synch class, is that there are checks for the Interrupt flag. This is really important in common library code that will be used billions of times. What this means is that if the Interrupt flag is set somewhere prior, CountDownLatch will respect it and will not go into any wait situations. This allows threads to not hang when the application is ended and all its threads should have been interrupted. I see this problem a lot when I go to shut down an application and am forced to kill the PID with -9 signal. The usual culprit is bad multithreaded code that does not properly handle the Interrupt nor check for it.

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68