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`.