StructuredTaskScope.Subtask (Java SE 23 & JDK 23)
Description: declaration: module: java.base, package: java.util.concurrent, class: 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 class:
  `StructuredTaskScopePREVIEW<T>`

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.

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 thrown by the subtask.
  `T`
  `get()`
  Returns the result of the subtask.
  `StructuredTaskScope.Subtask.StatePREVIEW`
  `state()`
  Returns the state of the subtask.
  `Callable<? extends T>`
  `task()`
  Returns the value returning task provided to the `fork` method.

- ## Method Details

  - ### task

    Callable<? extends T> task()
    Returns the value returning task provided to the `fork` method.
    API Note:
      Task objects with unique identity may be used for correlation by implementations of `handleComplete`PREVIEW.
    Returns:
      the value returning task provided to the `fork` method

  - ### state

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

  - ### get

    T get()
    Returns the result of the subtask.

    To ensure correct usage, if the scope owner forksPREVIEW a subtask, then it must join (with `join`PREVIEW or `joinUntil`PREVIEW) before it can obtain the result of the subtask.

    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 task scope owner and did not join after forking
    See Also:
      - `StructuredTaskScope.Subtask.State.SUCCESS`PREVIEW

  - ### exception

    Throwable exception()
    Returns the exception thrown by the subtask.

    To ensure correct usage, if the scope owner forksPREVIEW a subtask, then it must join (with `join`PREVIEW or `joinUntil`PREVIEW) before it can obtain the exception thrown by the subtask.

    Returns:
      the exception thrown by the subtask
    Throws:
      `IllegalStateException` - if the subtask has not completed, completed with a result, or the current thread is the task scope owner and did not join after forking
    See Also:
      - `StructuredTaskScope.Subtask.State.FAILED`PREVIEW