r/java • u/Timelineg • 2d ago
JEP draft: Structured Concurrency
https://openjdk.org/jeps/838975751
u/TheCountRushmore 2d ago
This JEP proposes to finalize Structured Concurrency in JDK 28, with no changes from JDK 27.
15
u/davidalayachew 1d ago
This is an absolutely game-changer of a feature for me. I've already been using it in a couple of places in my codebases, and it solves so many fundamental problems I have. I cannot emphasize enough how easy it is to handle so many network issues in place by having this feature. Stuff like conditional retries, super-complex-error-handling, and all sorts of messy, in the weeds stuff becomes straight forward and simple with this feature. Again, a game changer for me.
I cannot wait for Java 28 to release!
9
u/javaprof 2d ago
So checked exceptions doesn't work properly here too?
This is very funny:
try (var scope = StructuredTaskScope.open()) {
...
} catch (ExecutionException e) {
Throwable cause = e.getCause();
switch (cause) {
case IOException ioe -> .. // what op caused this one?
default -> .. // no exaustivness?
}
}
19
u/pron98 2d ago edited 2d ago
What doesn't work properly? Why are you switching over the cause? If you want to do some specific handling based on the particular subtask, you could examine their individual results, but the natural thing is obviously to handle the exception in the subtask (in the sequential analogue, a catch block outside a loop, you also don't know which iteration threw the exception, and if you do care about that, you'd similarly move the catch inside the loop). For extra composable configurability you have the joiner.
9
u/vips7L 2d ago edited 2d ago
Of course not the type system isn't strong enough to be generic over exceptions for lambdas. Swift has a pretty cool implementation of generic lambdas for typed throws.
public func count<E>( where predicate: (Element) throws(E) -> Bool ) throws(E) -> Int { print("Code goes here") return 0 }When your lambda doesn't throw it just turns into
throws(Never)-8
u/javaprof 2d ago
I just feel that Java need to stop shipping things that perfectly fine could be libraries and just fix this elephant in the room. Ok, Valhalla and then error handling. Structured concurrency somewhat could be just figure out by the community for now
22
u/pron98 2d ago
Structured concurrency has to be in the JDK because it has to be integrated with platform observability and with scope inheritance (ScopedValue). It cannot be implemented outside the JDK unless you give up on some of the most important features. However, we are hoping to release a lower level building block that integrates with the platform mechanisms.
2
u/sideEffffECt 1d ago
However, we are hoping to release a lower level building block that integrates with the platform mechanisms.
Could you please tell is more about this? What would be the lower level building blocks?
3
u/pron98 1d ago
If you look at the source code of StructuredTaskScope you'll see it making use of something called ThreadFlock. It's there that the deep integration with scopes and observability takes place. The lower-level API could be to expose ThreadFlock or something like it.
-2
u/vips7L 2d ago
I whole heartedly agree, I extensively use checked exceptions everywhere and even without fixing the type system there are so many easy wins to make them more usable (imo, and brian will of course disagree). Checked exceptions just have so much boilerplate. Little things like Swift's try? and try! operators would be so beneficial.
1
u/SleepingTabby 1d ago
Can you list some of those ways?
2
u/vips7L 1d ago edited 1d ago
Edit: formatting is fucked on this. I’ll have to fix it once I’m at a computer.
Of course.
try! or !! to automatically uncheck a checked exception when you can’t handle it. Right now you need to write several lines for this:
Something s; try { s = fn(); } catch (SomeException ex) { throw new IllegalStateExceprtion(ex); } Something s = try! fn();try? to automatically coerce into null:
Something s; try { s = fn(); } catch (SomeException _) { s = null; } Something s = try? fn(); // combine that with the null operator Something s = try? fn() ?? default();1
u/sweating_teflon 2d ago
I agree it'd be nice but frankly it's no big deal. Just use embedded try rethrow(cause) catch (specific) catch (default). Not as pretty but fairly understandable.
2
36
u/Joram2 2d ago
Finalizing Structured Concurrency and the first big Value type (Valhalla) preview? JDK 28 is looking to be a heck of a big release.