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).
Bonus! If you think of an integer as a vector with 32 bits, then compressing/expanding applies here, too. And the mask is 32 booleans/bits, i.e. also an integer. Hence, `Integer` got this static method: int compress(int i, int mask) Similarly, `expand` and same on `Long`.
#10: Suppress Doclint Warnings Do you write Javadoc? 👍 Do you use Doclint? 👍 Do you fix all warnings? 😬 You can now suppress Javadoc-related warnings, e.g.: // suppress all @SuppressWarnings( "doclint") // suppress syntax warnings @SuppressWarnings( "doclint:syntax")
#12: G1 Region Size G1 splits the heap into regions with (formerly) max size 32MB. This can cause performance issues with larges objects on large heaps and generally on very large heaps. Since #JDK18, region size max is 512 MB - set it with `-XX:G1HeapRegionSize`.
Security Performance 📈 : #13 2x SHA3 performance bugs.openjdk.org/browse/JDK-827… #14 cached TLS session certificate objects bugs.openjdk.org/browse/JDK-828… #15 `SSLAlgorithmConstraints` not evaluated twice bugs.openjdk.org/browse/JDK-828… #16 cached constraint check results bugs.openjdk.org/browse/JDK-828…
#17: Custom Localized Date-Time Formats You can have a `DateTimeFormatter` with a custom pattern: `ofPattern("y-MM-dd")` You can have a localized one, too: `ofLocalizedDate(FormatStyle.SHORT)` And since #Java19, custom localized one works, too: `ofLocalizedPattern("yMM")`
#18: Better Javadoc Search Since #JDK19, Javadoc's search box can handle multiple search terms. Rocket science, I know, but it's still good to have it!
#19: Javadoc Search Page Javadoc now has a dedicated URL for searches at docs.oracle.com/en/java/javase… Seems inconsequential, but can make a big difference. I immediately created a custom browser search, so "j stream" will take me directly to the search results for "stream". 👍