StructuredTaskScope.Subtask (Java SE 25 & JDK 25)
Description: declaration: module: java.base, package: java.util.concurrent, interface: StructuredTaskScope, interface: Subtask
Keywords: java.util.concurrent.StructuredTaskScope.Subtask interface

# Interface StructuredTaskScope.Subtask<T>

Type Parameters:
  `T` - the result type

All Superinterfaces:
  `Supplier<T>`

Enclosing interface:
  `StructuredTaskScopePREVIEW<T,R>`

public static sealed interface StructuredTaskScope.Subtask<T> extends Supplier<T>
`Subtask` is a preview API of the Java platform.
Programs can only use `Subtask` when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
Represents a subtask forked with `StructuredTaskScope.fork(Callable)`PREVIEW or `StructuredTaskScope.fork(Runnable)`PREVIEW.

Code that forks subtasks can use the `get()` method after joiningPREVIEW to obtain the result of a subtask that completed successfully. It can use the `exception()` method to obtain the exception thrown by a subtask that failed.

Since:
  21

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Interface
  Description
  `static enum`
  `StructuredTaskScope.Subtask.StatePREVIEW`
  Preview.
  Represents the state of a subtask.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Throwable`
  `exception()`
  Returns the exception or error thrown by this subtask if it failed.
  `T`
  `get()`
  Returns the result of this subtask if it completed successfully.
  `StructuredTaskScope.Subtask.StatePREVIEW`
  `state()`
  Returns the subtask state.

- ## Method Details

  - ### state

    StructuredTaskScope.Subtask.StatePREVIEW state()
    Returns the subtask state.
    Returns:
      the subtask state

  - ### get

    T get()
    Returns the result of this subtask if it completed successfully. If the subtask was forked with `fork(Callable)`PREVIEW then the result from the `call` method is returned. If the subtask was forked with `fork(Runnable)`PREVIEW then `null` is returned.

    Code executing in the scope owner thread can use this method to get the result of a successful subtask only after it has joinedPREVIEW.

    Code executing in the `Joiner` `onComplete`PREVIEW method should test that the subtask state is `SUCCESS`PREVIEW before using this method to get the result.

    Specified by:
      `get` in interface `Supplier<T>`
    Returns:
      the possibly-null result
    Throws:
      `IllegalStateException` - if the subtask has not completed, did not complete successfully, or the current thread is the scope owner invoking this method before joiningPREVIEW
    See Also:
      - `StructuredTaskScope.Subtask.State.SUCCESS`PREVIEW

  - ### exception

    Throwable exception()
    Returns the exception or error thrown by this subtask if it failed. If the subtask was forked with `fork(Callable)`PREVIEW then the exception or error thrown by the `call` method is returned. If the subtask was forked with `fork(Runnable)`PREVIEW then the exception or error thrown by the `run` method is returned.

    Code executing in the scope owner thread can use this method to get the exception thrown by a failed subtask only after it has joinedPREVIEW.

    Code executing in a `Joiner` `onComplete`PREVIEW method should test that the subtask state is `FAILED`PREVIEW before using this method to get the exception.

    Returns:
      the exception or error thrown by this subtask if it failed
    Throws:
      `IllegalStateException` - if the subtask has not completed, completed with a result, or the current thread is the scope owner invoking this method before joiningPREVIEW
    See Also:
      - `StructuredTaskScope.Subtask.State.FAILED`PREVIEW