Scope: Structured Concurrency

Start tasks together, end them together, and join their results the way you choose.

"Here's the core idea: every time our control splits into multiple concurrent paths, we want to make sure that they join up again."

— Nathaniel J. Smith, Notes on structured concurrency, or: Go statement considered harmful

A Scope is where they join up: it forks its tasks, and the VTask from its join() ends only when every task has finished or been cancelled.

What You'll Learn

  • Fork tasks into a Scope and join them, so none outlives the scope that started it
  • Choose how they are joined: every result, the first success, the first to finish, or every failure
  • Hold a scope's failure as a value, and time the whole group out, cancelling the tasks still running
  • Run one Scope again, retry it, or share it between threads
  • Use a ScopeJoiner with a Scope, or with a StructuredTaskScope you open yourself

See Example Code

The code on this page is ScopeBook.java: the page includes it, so the build compiles and runs it. ScopeBookTest.java holds what the page says each method does. ScopeExample.java is a longer example.


Tasks that start and end together

A task handed to an ExecutorService can outlive the method that submitted it, and a failure in one task leaves the others running. Java 25's StructuredTaskScope (JEP 505) ties tasks to a block instead, as Nathaniel J. Smith's essay argued for: they are forked inside a try, and the block does not end until each has finished. A Scope holds the same shape as a value. You fork VTasks into it, and join() gives a VTask that, when it runs, opens a StructuredTaskScope, forks each task on a virtual thread of its own, and waits for them.

A preview API

Scope and ScopeJoiner are built on StructuredTaskScope, a preview feature in Java 25, so code that uses them needs --enable-preview to compile and to run, as the VTask page explains.

The code on this page uses these imports, with Order and LineItem from the order records the Optics chapter introduces:

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import org.higherkindedj.example.book.optics.cast.LineItem;
import org.higherkindedj.example.book.optics.cast.Order;
import org.higherkindedj.hkt.validated.Validated;
import org.higherkindedj.hkt.vtask.Scope;
import org.higherkindedj.hkt.vtask.VTask;

Booking delivery for an order means asking couriers for a quote, and checking that the warehouse has the stock. Each call blocks until its service answers:

  // A courier quotes for an order, and a warehouse counts its stock of a product. Each call
  // blocks until its service answers, and may throw an IOException.
  interface Courier {
    BigDecimal quote(Order order) throws IOException;
  }

  interface Warehouse {
    int inStock(String sku) throws IOException;
  }

Scope.allSucceed() waits for every task and gives their results in the order they were forked. Scope.anySucceed() gives the first success, and Scope.firstComplete() the first task to finish, success or failure; both cancel the tasks still running. forkAll forks a list of tasks, and fork one:

  static VTask<BigDecimal> quoteFrom(Courier courier, Order order) {
    return VTask.of(() -> courier.quote(order));
  }

  static List<VTask<BigDecimal>> quotesFrom(List<Courier> couriers, Order order) {
    return couriers.stream().map(courier -> quoteFrom(courier, order)).toList();
  }

  // Every courier's quote, in the order they were forked
  static VTask<List<BigDecimal>> allQuotes(List<Courier> couriers, Order order) {
    return Scope.<BigDecimal>allSucceed().forkAll(quotesFrom(couriers, order)).join();
  }

  // The first courier to answer with a quote
  static VTask<BigDecimal> firstQuote(List<Courier> couriers, Order order) {
    return Scope.<BigDecimal>anySucceed().forkAll(quotesFrom(couriers, order)).join();
  }

  // Whichever courier answers first, with a quote or a failure
  static VTask<BigDecimal> firstAnswer(List<Courier> couriers, Order order) {
    return Scope.<BigDecimal>firstComplete().forkAll(quotesFrom(couriers, order)).join();
  }

ORDER is an order for two lamps and a shade. COURIERS holds three couriers, which quote 7.99, 6.50 and 9.20, and DOWN stands in for a courier whose service is down:

    List<BigDecimal> quotes = allQuotes(COURIERS, ORDER).run();
    // [7.99, 6.50, 9.20]
    BigDecimal answered = firstQuote(List.of(DOWN, COURIERS.get(1), DOWN), ORDER).run();
    // 6.50

Building a scope runs nothing, and each run forks its tasks afresh.

Choosing a joiner

Java's StructuredTaskScope takes a Joiner, which decides when the scope is done and what its join() returns. A Scope's factory chooses one:

FactoryGivesWhen a task failsUse it for
allSucceed()Every result, in fork orderCancels the rest, and fails with its exceptionLookups that must all succeed
anySucceed()The first success, cancelling the restFails only if every task fails, with one of their exceptionsRedundant requests, any one of which will do
firstComplete()The first task to finish, success or failure, cancelling the restFails if that first task failedTaking whichever answer comes first
accumulating(mapper)A Validated: every result, or every failureKeeps it, mapped by mapper, and waits for the restChecks that should all report
withJoiner(joiner)What the ScopeJoiner decidesAs the joiner decidesOne joining policy shared by several scopes

For a fallback that answers when the fast path fails, use anySucceed: firstComplete takes a failure if it comes first.

The scope is opened on the thread that runs the VTask, so its tasks see that thread's ScopedValue bindings, such as the request context. named(...) gives the scope a name, which a thread dump in JSON format shows.

Keeping every failure

A form, or an order, that fails on its first problem sends the user away to fix it, and back to find the next. Scope.accumulating(mapper) runs every task to the end, cancelling none, and gives a Validated: every result if all succeeded, or every failure, each mapped by mapper, in fork order. Checking each line of the order against the warehouse:

  static VTask<LineItem> checkStock(Warehouse warehouse, LineItem line) {
    return VTask.of(
        () -> {
          int inStock = warehouse.inStock(line.sku());
          if (inStock < line.quantity()) {
            throw new IllegalStateException(
                line.sku() + ": " + line.quantity() + " ordered, " + inStock + " in stock");
          }
          return line;
        });
  }

  // Every line is checked, and every shortfall kept
  static VTask<Validated<List<String>, List<LineItem>>> checkLines(
      Warehouse warehouse, Order order) {
    return Scope.<String, LineItem>accumulating(Throwable::getMessage)
        .forkAll(order.lines().stream().map(line -> checkStock(warehouse, line)).toList())
        .join();
  }

SHORT stands in for a warehouse with one lamp and no shades, and WELL_STOCKED for one with plenty of both:

    Validated<List<String>, List<LineItem>> shortfalls = checkLines(SHORT, ORDER).run();
    // Invalid([LAMP: 2 ordered, 1 in stock, SHADE: 1 ordered, 0 in stock])
    boolean allInStock = checkLines(WELL_STOCKED, ORDER).run().isValid();
    // true

Both shortfalls are reported at once. Each scope calls mapper on the thread that joins it, so a mapper shared by scopes running at once must be thread-safe.

Holding the result as a value

join() gives a task that fails when the scope does. joinSafe(), joinEither() and joinMaybe() give one that always succeeds, holding the failure as a Try, an Either with the exception on the left, or a Maybe that is Nothing. Use them to handle a failure inside the pipeline, rather than where the task is run. These are the extra imports:

import org.higherkindedj.hkt.either.Either;
import org.higherkindedj.hkt.trymonad.Try;
    Either<Throwable, List<BigDecimal>> refused =
        Scope.<BigDecimal>allSucceed().fork(quoteFrom(DOWN, ORDER)).joinEither().run();
    // Left(java.io.IOException: the courier is down)

Timing a scope out

timeout(duration) bounds the whole scope. When it expires, the tasks still running are interrupted, those not yet started never start, and the run fails with a TimeoutException once they have stopped. SILENT stands in for a courier that never answers. This is the extra import:

import java.time.Duration;
    Try<List<BigDecimal>> late =
        Scope.<BigDecimal>allSucceed()
            .timeout(Duration.ofMillis(50))
            .fork(quoteFrom(SILENT, ORDER))
            .joinSafe()
            .run();
    // Failure(java.util.concurrent.TimeoutException: Scope timed out after PT0.05S)

A scope's timeout cancels the tasks still running, as VTask.timeout cancels its one task. A task that ignores interruption, such as one spinning in a loop, holds the failure back until it finishes. Each run's timeout starts when that run opens its scope. A timeout of zero or less expires at once, so the run fails unless its tasks have already finished.

Running a scope again

A Scope is a description, like the VTask its join() returns. Each run of that VTask opens a StructuredTaskScope of its own, with a new Joiner. So you can run it again, retry it, or run it from several threads at once, and each run joins only the tasks it forked.

Here the quote service is busy on its first call, and Retry runs the same prices task again. These are the extra imports:

import java.util.concurrent.atomic.AtomicInteger;
import org.higherkindedj.hkt.resilience.Retry;
import org.higherkindedj.hkt.resilience.RetryPolicy;
    AtomicInteger quoteCalls = new AtomicInteger();
    VTask<String> quote =
        VTask.of(
            () -> {
              if (quoteCalls.incrementAndGet() == 1) {
                throw new IllegalStateException("quote service busy");
              }
              return "quote";
            });
    VTask<List<String>> prices =
        Scope.<String>allSucceed().fork(quote).fork(VTask.succeed("rates")).join();

    List<String> retried =
        Retry.retryTask(prices, RetryPolicy.fixed(3, Duration.ofMillis(100))).run();
    // [quote, rates]
    List<String> runAgain = prices.run();
    // [quote, rates]

The first attempt fails. The second forks both tasks again and joins only those, so the first attempt's failure cannot fail it, and it returns [quote, rates]. Running prices once more gives the same list. A retry runs every task again, rates included, so retry a Scope whose tasks are safe to repeat.

A joiner of your own

A ScopeJoiner is a joining policy on its own: ScopeJoiner.allSucceed(), anySucceed(), firstComplete() and accumulating(mapper) match the Scope factories. Scope.withJoiner takes one, and ScopeJoiner.joiner() builds the JDK's Joiner for a scope you open yourself. These are the extra imports:

import java.util.concurrent.StructuredTaskScope;
import org.higherkindedj.hkt.vtask.ScopeJoiner;

twoQuotesByHand opens a scope with a Joiner from the policy, as Java's own API asks:

  static List<BigDecimal> twoQuotesByHand(
      ScopeJoiner<BigDecimal, List<BigDecimal>> policy, Order order) throws InterruptedException {
    try (var scope = StructuredTaskScope.open(policy.joiner())) {
      scope.fork(() -> COURIERS.get(0).quote(order));
      scope.fork(() -> COURIERS.get(1).quote(order));
      return scope.join();
    }
  }

The same policy serves a Scope and the scope opened by hand:

    ScopeJoiner<BigDecimal, List<BigDecimal>> everyQuote = ScopeJoiner.allSucceed();

    // The same policy for a Scope, and for the StructuredTaskScope opened by hand
    List<BigDecimal> viaScope =
        Scope.withJoiner(everyQuote).forkAll(quotesFrom(COURIERS, ORDER)).join().run();
    // [7.99, 6.50, 9.20]
    List<BigDecimal> byHand = twoQuotesByHand(everyQuote, ORDER);
    // [7.99, 6.50]

A ScopeJoiner holds its configuration, not a scope's state. Each call to joiner() returns a new Joiner, because a StructuredTaskScope needs one that no other scope has used, so one ScopeJoiner serves any number of scopes.

ScopeJoiner.firstSuccessEither() races tasks that each give an Either. The first Right wins and cancels the rest. A Left does not end the race; if every task gives one, the join gives a Left of all their errors, in fork order. A task that throws is not collected: with no Right, the join fails with its exception. VResultPath.firstSuccess races typed paths through it.

ScopeJoiner.resultEither() is deprecated, and scheduled for removal in 0.6.0. Use Scope.joinEither() instead, or, with a scope you open yourself, the value its join() returns, or the cause of the FailedException it throws.

Methods

MethodWhat it does
Scope.allSucceed(), anySucceed(), firstComplete(), accumulating(mapper), withJoiner(joiner)Build a scope that joins its tasks as Choosing a joiner says
fork(task), forkAll(tasks)Give a new scope with the tasks added, running nothing
timeout(duration), named(name)Give a new scope that times out, or carries a name for thread dumps
join()Gives a VTask that runs the scope and gives the joined result, or fails
joinSafe(), joinEither(), joinMaybe()Give a VTask that holds a failure as a Try, an Either or a Maybe
taskCount(), hasTimeout(), getTimeout()Say how many tasks are forked, and whether a timeout is set
ScopeJoiner.allSucceed() and the others, firstSuccessEither()Give a joining policy, for withJoiner or a scope you open yourself
joiner.joiner()Gives a new JDK StructuredTaskScope.Joiner

When to use a Scope

You needUse
Tasks that must all succeed, joined into one valuePar, or parZipWith on a VTaskPath
Tasks joined by first success, first to finish or every failure, or under one timeoutScope
Typed results raced or gathered on a railwayVResultPath
A resource held while the scope's tasks use itResource with a Scope

Hands-On Learning

Practise Scope in TutorialScope (10 exercises), part of the Scope & Resource journey.


Previous: VTask Next: Resource Management