I need to wake up or send to sleep a single Thread sometimes, and I'm wondering what is the best and most efficient way to do it.
The first solution is signaling combined with wait-notify
(I know how to implement this pattern properly, that's not the question).
I read it somewhere that using the java.concurrent library and CountDownLatch
for signaling is more efficient. I checked concurrent.locks.Condition as well, but this topic states that it's merely a (programmer-wise) more safe and generalized construct, without a performance benefit compared to notify/notifyAll
. Peter Lawrey recommends using the Concurrency library instead of notify-notifyAll
in this comment, so now I'm confused what is the best practice to use.
A related question: which is better performance-wise, notify
or notifyAll
in my case (i.e. if I have one thread)? I know there are lot of similar threads about this, but none of them give a clear answer. In my case, functionally, it doesn't matter which I use, but I wonder which is faster, then.