koreth,

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.

JackbyDev,

Those sound useful. I haven't had to do much cross-thread synchronization thankfully, but I have had to use a https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/concurrent/BlockingDeque.html to check that some events came and in the right order. The CountDownLatch and Phaser may have been better.

austin,

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)?

<pre style="background-color:#ffffff;">
<span style="color:#323232;">class LongRunningAction implements Runnable {
</span><span style="color:#323232;">    private String threadName;
</span><span style="color:#323232;">    private Phaser ph;
</span><span style="color:#323232;">
</span><span style="color:#323232;">    LongRunningAction(String threadName, Phaser ph) {
</span><span style="color:#323232;">        this.threadName = threadName;
</span><span style="color:#323232;">        this.ph = ph;
</span><span style="color:#323232;">        ph.register();
</span><span style="color:#323232;">    }
</span><span style="color:#323232;">
</span><span style="color:#323232;">    @Override
</span><span style="color:#323232;">    public void run() {
</span><span style="color:#323232;">        ph.arriveAndAwaitAdvance();
</span><span style="color:#323232;">        try {
</span><span style="color:#323232;">            Thread.sleep(20);
</span><span style="color:#323232;">        } catch (InterruptedException e) {
</span><span style="color:#323232;">            e.printStackTrace();
</span><span style="color:#323232;">        }
</span><span style="color:#323232;">        ph.arriveAndDeregister();
</span><span style="color:#323232;">    }
</span><span style="color:#323232;">}
</span>

then

<pre style="background-color:#ffffff;">
<span style="color:#323232;">executorService.submit(new LongRunningAction("thread-1", ph));
</span><span style="color:#323232;">executorService.submit(new LongRunningAction("thread-2", ph));
</span><span style="color:#323232;">executorService.submit(new LongRunningAction("thread-3", ph));
</span>

if ph.arriveAndAwaitAdvance(); is called before all of the LongRunningActions are initialized, won’t it proceed before it is supposed to?

pohart,

Your analysis looks right to me. If this were mine I'd initialize all three before submitting any.

fabian,

Not sure if it counts as not widely used, but I think the Collections.unmodifiable(Collection|Set|List...) methods are very useful.

grossjonas,

PushbackInputStream - makes it easy to write a small template engine in restricted environments

pohart,

Ooh, I've never seen that before. And it's been there since 1.0

JackbyDev,

Link for the curious

A PushbackInputStream adds functionality to another input stream, namely the ability to "push back" or "unread" bytes, by storing pushed-back bytes in an internal buffer. This is useful in situations where it is convenient for a fragment of code to read an indefinite number of data bytes that are delimited by a particular byte value; after reading the terminating byte, the code fragment can "unread" it, so that the next read operation on the input stream will reread the byte that was pushed back. For example, bytes representing the characters constituting an identifier might be terminated by a byte representing an operator character; a method whose job is to read just an identifier can read until it sees the operator and then push the operator back to be re-read.

This does sound useful. I have to admit I don't know much about InputStream and OutputStream and every time I need to do something with them (which is rare) I just search it up.

pohart,

Optional! I was reluctant at first because of the nullibility, but it's so useful.

I'm not sure this is what you were going for. I think most people know about optional and are skeptical because it's not a fix for nullability.

BitPirate,

Don't forget its subtypes. OptionalInt, OptionalDouble etc. This avoids auto(un)boxing.

pohart,

Oh wow that's great. I didn't know about those at all.

JackbyDev,

I didn't have a specific goal, but yeah I do wish more people used Optional.

My one "gripe" with Optional (and I use it lightly) is that they mean for it to only be used as a return type instead of anywhere something can be optional. It can still be used as that though, they just don't recommend it.

pohart,

IMHO it should be used for parameters, as long as there is more than one optional parameter. If rather all non nullable parameters though, which is usually doable.

JackbyDev,

I've waffled on it. Currently my opinion is to use whatever nullable annotation the project uses (if any, otherwise JetBrains's). Essentially, I'm not sure if the official recommendation to avoid Optional for uses other than return types has reasoning I'm missing.

I do use it like this though and lament that we don't have an Elvis operator (

Optional.ofNullable(thingThatMightBeNull).map(e -> e.someMethod()).orElse(null);

kaba0,

HttpClient. You don’t necessarily need a third-party tool for rest calls or the like, and it really is quite handy!

Sheldan,

Finally a more sane API for HTTP requests in java. Still using a library tho, can be very convenient (like okhttp)

  • All
  • Subscribed
  • Moderated
  • Favorites
  • java@programming.dev
  • DreamBathrooms
  • ngwrru68w68
  • osvaldo12
  • magazineikmin
  • tacticalgear
  • rosin
  • thenastyranch
  • Youngstown
  • Durango
  • slotface
  • everett
  • kavyap
  • InstantRegret
  • khanakhh
  • provamag3
  • mdbf
  • ethstaker
  • cisconetworking
  • tester
  • Leos
  • cubers
  • GTA5RPClips
  • megavids
  • normalnudes
  • anitta
  • modclub
  • JUstTest
  • lostlight
  • All magazines