JackbyDev@programming.devM to Java@programming.dev · 2 years agoWhat are some things in the class library you wish more people knew about or used?docs.oracle.comexternal-linkmessage-square12fedilinkarrow-up12arrow-down10file-text
arrow-up12arrow-down1external-linkWhat are some things in the class library you wish more people knew about or used?docs.oracle.comJackbyDev@programming.devM to Java@programming.dev · 2 years agomessage-square12fedilinkfile-text
I’m curious if there are things in the standard class library that you find useful but not widely used.
minus-squareaustin@programming.devlinkfedilinkEnglisharrow-up1·1 year agoI 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)? class LongRunningAction implements Runnable { private String threadName; private Phaser ph; LongRunningAction(String threadName, Phaser ph) { this.threadName = threadName; this.ph = ph; ph.register(); } @Override public void run() { ph.arriveAndAwaitAdvance(); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } ph.arriveAndDeregister(); } } then executorService.submit(new LongRunningAction("thread-1", ph)); executorService.submit(new LongRunningAction("thread-2", ph)); executorService.submit(new LongRunningAction("thread-3", ph)); if ph.arriveAndAwaitAdvance(); is called before all of the LongRunningActions are initialized, won’t it proceed before it is supposed to?
minus-squarepohart@lemmyrs.orglinkfedilinkEnglisharrow-up2·1 year agoYour analysis looks right to me. If this were mine I’d initialize all three before submitting any.
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)?
class LongRunningAction implements Runnable { private String threadName; private Phaser ph; LongRunningAction(String threadName, Phaser ph) { this.threadName = threadName; this.ph = ph; ph.register(); } @Override public void run() { ph.arriveAndAwaitAdvance(); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } ph.arriveAndDeregister(); } }
then
executorService.submit(new LongRunningAction("thread-1", ph)); executorService.submit(new LongRunningAction("thread-2", ph)); executorService.submit(new LongRunningAction("thread-3", ph));
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.