this post was submitted on 16 Jun 2023
2 points (100.0% liked)

Java

1527 readers
4 users here now

For discussing Java, the JVM, languages that run on the JVM, and other related technologies.

founded 2 years ago
MODERATORS
 

I'm curious if there are things in the standard class library that you find useful but not widely used.

top 11 comments
sorted by: hot top controversial new old
[–] [email protected] 1 points 2 years ago

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

[–] [email protected] 1 points 2 years ago

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

[–] [email protected] 0 points 2 years ago (1 children)

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.

[–] [email protected] 0 points 2 years ago (1 children)

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.

[–] [email protected] 0 points 2 years ago (1 children)

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.

[–] [email protected] 0 points 2 years ago (1 children)

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);
[–] [email protected] 0 points 2 years ago (1 children)

Instead of wrapping it in an optional you can do Objects.requireNonNullElse(value, defaultValue)

[–] [email protected] 1 points 2 years ago* (last edited 2 years ago)

Optional has more syntactic sugar for more complex scenarios / functional call chaining that prevents repetitive if checks

Optional.ofNullable(myObj)
  .map(MyClass::getProperty)
  .map(MyOtherClass::getAnotherProperty)
  .filter(obj -> somePredicate(obj))
  .orElse(null)

This is completely null safe, the function calls are only made if the object is not null

[–] [email protected] 0 points 2 years ago (1 children)

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.

[–] [email protected] 1 points 2 years ago (1 children)

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 the LongRunningActions are initialized, won't it proceed before it is supposed to?

[–] [email protected] 2 points 2 years ago

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

load more comments
view more: next ›