By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: JEP 505 Delivers Fifth Preview of Java’s Structured Concurrency with Key API Refinements
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > News > JEP 505 Delivers Fifth Preview of Java’s Structured Concurrency with Key API Refinements
News

JEP 505 Delivers Fifth Preview of Java’s Structured Concurrency with Key API Refinements

News Room
Last updated: 2025/05/12 at 11:12 AM
News Room Published 12 May 2025
Share
SHARE

JEP 505, Structured Concurrency (Fifth Preview), has reached Targeted status in the JDK 25 release. The API, which has evolved through five preview iterations, aims to simplify and provide developers with clearer, safer frameworks for managing parallel tasks, particularly when working with virtual threads. The latest preview refines the API introduced in earlier incubations (JEP 428, JEP 437) and previews (JEP 453, JEP 462, JEP 480, JEP 499). Most notably, StructuredTaskScope is no longer instantiated via public constructors. Developers now open a scope through static factory methods, such as StructuredTaskScope.open(), a change that clarifies defaults and paves the way for richer completion policies.

According to the JEP specification, structured concurrency addresses three fundamental concerns in parallel programming:

  • Keeping subtask lifetimes strictly confined to a well-defined parent scope
  • Implementing reliable cancellation that prevents resource leaks
  • Enhancing observability through structured thread hierarchies

The API centers around the java.util.concurrent.StructuredTaskScope class, which manages a group of concurrent subtasks. Developers fork subtasks within a scope and then join them together, with the scope automatically managing their execution boundaries.

For example, consider the following coding snippets:


try (var scope = StructuredTaskScope.open()) {
    Subtask<String> user = scope.fork(() -> fetchUser(userId));
    Subtask<List<Order>> orders = scope.fork(() -> fetchOrders(userId));
    
    scope.join();  // Wait for all subtasks
    
    // Process results or handle exceptions
    String userName = user.get();
    List<Order> userOrders = orders.get();
}

This example demonstrates the fundamental usage pattern of structured concurrency. It creates a scope using the new factory method, forks two concurrently executed tasks (fetching user data and orders), waits for both to complete with join(), and then retrieves their results. The scope guarantees that both subtasks are either completed or cancelled when execution leaves the block.

For contrast, here’s how the same code would have looked in previous previews of the Structured Concurrency API:


try (var scope = new StructuredTaskScope<>()) {  
    Subtask<String> user = scope.fork(() -> fetchUser(userId));
    Subtask<List<Order>> orders = scope.fork(() -> fetchOrders(userId));
    
    scope.join();
    
    // Process results or handle exceptions
    String userName = user.get();
    List<Order> userOrders = orders.get();
}

While the basic structure remains similar, the fifth preview introduces factory methods like StructuredTaskScope.open() replaces the constructor-based instantiation. This change improves API readability and gives library maintainers more flexibility for future evolution without breaking compatibility.

The zero-argument open() factory creates a scope that fails fast: if any subtask throws, the remaining ones are interrupted and join() rethrows. Developers can supply custom policies via open(Joiner), for example:


// Return the first successful result, cancel the rest
<T> T race(Collection<Callable<T>> tasks) throws InterruptedException {
    try (var scope = StructuredTaskScope.open(
            Joiner.<T>anySuccessfulResultOrThrow())) {
        tasks.forEach(scope::fork);
        return scope.join();
    }
}

Each fork launches a subtask, by default on a virtual thread, returning a Subtask handle whose get() is safe only after join() completes. The scope enforces structure: calls to fork or join from non-owner threads, or exiting the block without closing, raise StructureViolationException.

The factory method allSuccessfulOrThrow() returns a new joiner that, when all subtasks complete successfully, yields a stream of the subtasks:


<T> List<T> runConcurrently(Collection<Callable<T>> tasks) throws InterruptedException {
    try (var scope = StructuredTaskScope.open(Joiner.<T>allSuccessfulOrThrow())) {
        tasks.forEach(scope::fork);
        return scope.join().map(Subtask::get).toList();
    }
}

If one or more subtasks fail, then join() throws a FailedException, with the exception from one of the failed subtasks as its cause.

The Joiner interface declares three additional factory methods. The awaitAll() method returns a new joiner that waits for all subtasks to complete, whether successfully or not. The awaitAllSuccessfulOrThrow() method returns a new joiner that waits for all subtasks to complete successfully. Lastly, allUntil(Predicate<Subtask<? extends T>> isDone) returns a new joiner that, when all subtasks complete successfully or else a predicate on a completed subtask returns true, cancels the enclosing scope and yields a stream of all the subtasks.

When using any Joiner, it is critical to create a new Joiner for each StructuredTaskScope. Joiner objects should never be used in different task scopes or reused after a scope is closed.

The Joiner interface can be implemented directly to support custom completion policies. It has two type parameters: T for the result type of the subtasks executed in the scope, and R for the result type of the join() method. The interface looks as follows:


public interface Joiner<T, R> {
    public default boolean onFork(Subtask<? extends T> subtask);
    public default boolean onComplete(Subtask<? extends T> subtask);
    public R result() throws Throwable;
}

The onFork() method is invoked when forking a subtask, while the onComplete() method is invoked when a subtask completes.

The specification also clarifies that a scope’s subtasks inherit ScopedValue bindings. If a scope’s owner reads a value from a bound ScopedValue then each subtask will read the same value.

This JEP also extends the JSON thread-dump format added for virtual threads to show how StructuredTaskScopes group threads into a hierarchy:


$jcmd <pid> Thread.dump_to_file -format=json <file>

The JSON object for each scope contains an array of the threads forked in the scope, together with their stack traces.

As a preview feature, the OpenJDK team encourages developers to experiment with this fifth iteration in JDK 25 and provide feedback. This input is vital for maturing the API.

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Using social media for recruitment: Attract top talent quickly
Next Article SanDisk’s massive 1.5TB microSD card is now less than £90
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Bright auroras on Jupiter are captured by Webb Space Telescope
News
Asus presents a PC for AI, Expertcenter Pro Et900n G3
Mobile
‘Landman’ season 2 release window revealed — here’s what we know so far
News
10 Best Product Launch Software for Successful Campaigns
Computing

You Might also Like

Bright auroras on Jupiter are captured by Webb Space Telescope

1 Min Read
News

‘Landman’ season 2 release window revealed — here’s what we know so far

4 Min Read
News

Make the internet a safer place for the whole family with AdGuard, now A$25 for life

2 Min Read
News

The Paper, Peacock’s sequel to The Office, debuts in September

2 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?