Who needs chocolate when they can have Java features in their advent calendar? (Yes, I'm late, but fashionably so. 😋) From language features to API additions, from tooling to performance, here are 24 Java features you missed in 2022! As 📺: youtube.com/watch?v=ghGvFc… As 🧵: 👇🏾
#1: Structured Concurrency Debugging When you do structured concurrency, you create parent-child relationships between your threads that are visible in thread dumps and to debuggers. See @intellijidea screenshot what that can look like. This is a game changer for debugging!
#2: `Future` The interface got 3 new methods: * `exceptionNow()` to immediately return the task's exception * `resultNow()` to immediately return the task's result * `state()`: CANCELLED, FAILED, RUNNING, or SUCCESS First two throw `IllegalStateException` if in wrong state.
#3: `ForkJoinTask` 2 new methods: * `quietlyJoin(long timeout, TimeUnit unit)` tries to join the task before the given timeout * `quietlyJoinUninterruptibly(long timeout, TimeUnit unit)` same but can't be interrupted (Tbh, the 2nd one seems shady, but that's just gut feeling.)
#4: `ForkJoinPool` 2 new methods: * `lazySubmit(ForkJoinTask<T> task)` to submit tasks that you don't *need* to execute if contention is high (i.e. they may never run) * `setParallelism` to set the pool's target parallelism after creation 1st one is very interesting.
#5: `newHashSet` The `HashSet` constructor that takes an `int` argument interprets that as capacity, not number of expected elements. Once number of elements reaches $LOAD-FACTOR (by default 75%) of capacity, the set resizes itself. To avoid that resize with the constructor...
#6: `newHashMap` ... you either need to compute capacity from number of expected elements or set the load factor accordingly. Or, since #JDK19, call `HashSet::newHashSet` and pass the number of expected elements. Likewise, there's `HashMap::newHashMap`.
I need to take a break here. I did not get enough sleep last night (editing the video took too long) and am too tired to tweet more. 🥱 I'll keep adding to this thread over the next days until I catch up with the calendar 🗓️ and will then continue with one tweet per day...
#7: GitHub Action oracle-actions/setup-java works like actions/setup-java with a few differences: * supports EA builds from projects like Loom, Panama, etc. * better handling of EA builds in general * support for Oracle JDK More details: inside.java/2022/03/11/set…
#8: `Vector::compress` In #JDK19, `compress` was added to the incubating vector API. Called on a vector with a mask... var reds = rgbVector .compress(redMask); ... it selects the lanes where the mask is true and stores them in a contiguous block in the result vector.
#9: `Vector::expand` This is the dual operation to `compress`. Starting at index zero of the input vector, it stores each value in the output vector's lane at the next position where the mask is true. Other lanes are set to zero (oh, `compress` also sets remainder to zero).