16
What are some things in the class library you wish more people knew about or used?
(docs.oracle.com)
For discussing Java, the JVM, languages that run on the JVM, and other related technologies.
Sometimes people who build low-level code with mutable state that's shared across threads don't know about some of the synchronization tools Java has. A lot of people know about
synchronized
, a lot of people know about semaphores, but fewer people seem to know about CountDownLatch and fewer still seem to have heard about Phaser. Those last two have saved me from having to implement my own synchronization code on a number of occasions.Those sound useful. I haven't had to do much cross-thread synchronization thankfully, but I have had to use a
BlockingDeque
to check that some events came and in the right order. TheCountDownLatch
andPhaser
may have been better.I had never heard of Phaser, but it looks pretty cool. I just read Baeldung's Guide to Phaser and correct me if I'm wrong, but doesn't it kind of seem like a race condition (it could just be how they use it in the examples)?
then
if
ph.arriveAndAwaitAdvance();
is called before all of theLongRunningAction
s are initialized, won't it proceed before it is supposed to?Your analysis looks right to me. If this were mine I'd initialize all three before submitting any.