Class StructuredTaskScope.ShutdownOnSuccess<T>
- Type Parameters:
T
- the result type
- All Implemented Interfaces:
AutoCloseable
- Enclosing class:
StructuredTaskScope<T>
StructuredTaskScope
that captures the result of the first subtask to
complete successfully. Once captured, it invokes the shutdown
method to interrupt unfinished threads and wakeup the owner. The policy
implemented by this class is intended for cases where the result of any subtask
will do ("invoke any") and where the results of other unfinished subtask are no
longer needed.
Unless otherwise specified, passing a null
argument to a method
in this class will cause a NullPointerException
to be thrown.
- Since:
- 19
-
Nested Class Summary
Nested classes/interfaces declared in class jdk.incubator.concurrent.StructuredTaskScope
StructuredTaskScope.ShutdownOnFailure, StructuredTaskScope.ShutdownOnSuccess<T>
-
Constructor Summary
ConstructorDescriptionConstructs a new unnamedShutdownOnSuccess
that creates virtual threads.ShutdownOnSuccess
(String name, ThreadFactory factory) Constructs a newShutdownOnSuccess
with the given name and thread factory. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
handleComplete
(Future<T> future) Shut down the given task scope when invoked for the first time with aFuture
for a task that completed with a result.join()
Wait for all threads to finish or the task scope to shut down.Wait for all threads to finish or the task scope to shut down, up to the given deadline.result()
Returns the result of the first subtask that completed with a result.Returns the result of the first subtask that completed with a result, otherwise throws an exception produced by the given exception supplying function.Methods declared in class jdk.incubator.concurrent.StructuredTaskScope
close, fork, shutdown
-
Constructor Details
-
ShutdownOnSuccess
Constructs a newShutdownOnSuccess
with the given name and thread factory. The task scope is optionally named for the purposes of monitoring and management. The thread factory is used tocreate
threads when tasks are forked. The task scope is owned by the current thread.This method captures the current thread's scoped value bindings for inheritance by threads created in the task scope. The Tree Structure section in the class description details how parent-child relations are established implicitly for the purpose of inheritance of scoped value bindings.
- Parameters:
name
- the name of the task scope, can be nullfactory
- the thread factory
-
ShutdownOnSuccess
public ShutdownOnSuccess()Constructs a new unnamedShutdownOnSuccess
that creates virtual threads.This constructor is equivalent to invoking the 2-arg constructor with a name of
null
and a thread factory that creates virtual threads.
-
-
Method Details
-
handleComplete
Shut down the given task scope when invoked for the first time with aFuture
for a task that completed with a result.- Overrides:
handleComplete
in classStructuredTaskScope<T>
- Parameters:
future
- the completed task- See Also:
-
join
Wait for all threads to finish or the task scope to shut down. This method waits until all threads started in the task scope finish execution (of both task andhandleComplete
method), theshutdown
method is invoked to shut down the task scope, or the current thread is interrupted.This method may only be invoked by the task scope owner.
- Overrides:
join
in classStructuredTaskScope<T>
- Returns:
- this task scope
- Throws:
IllegalStateException
- if this task scope is closedWrongThreadException
- if the current thread is not the ownerInterruptedException
- if interrupted while waiting
-
joinUntil
public StructuredTaskScope.ShutdownOnSuccess<T> joinUntil(Instant deadline) throws InterruptedException, TimeoutException Wait for all threads to finish or the task scope to shut down, up to the given deadline. This method waits until all threads started in the task scope finish execution (of both task andhandleComplete
method), theshutdown
method is invoked to shut down the task scope, the current thread is interrupted, or the deadline is reached.This method may only be invoked by the task scope owner.
- Overrides:
joinUntil
in classStructuredTaskScope<T>
- Parameters:
deadline
- the deadline- Returns:
- this task scope
- Throws:
IllegalStateException
- if this task scope is closedWrongThreadException
- if the current thread is not the ownerInterruptedException
- if interrupted while waitingTimeoutException
- if the deadline is reached while waiting
-
result
Returns the result of the first subtask that completed with a result.When no subtask completed with a result but a task completed with an exception then
ExecutionException
is thrown with the exception as the cause. If only cancelled subtasks were notified to thehandleComplete
method thenCancellationException
is thrown.- API Note:
- This method is intended to be invoked by the task scope owner after it
has invoked
join
(orjoinUntil
). A future release may add enforcement to prevent the method being called by other threads or before joining. - Returns:
- the result of the first subtask that completed with a result
- Throws:
ExecutionException
- if no subtasks completed with a result but a subtask completed with an exceptionCancellationException
- if all subtasks were cancelledIllegalStateException
- if the handle method was not invoked with a completed subtask
-
result
Returns the result of the first subtask that completed with a result, otherwise throws an exception produced by the given exception supplying function.When no subtask completed with a result but a subtask completed with an exception then the exception supplying function is invoked with the exception. If only cancelled subtasks were notified to the
handleComplete
method then the exception supplying function is invoked with aCancellationException
.- API Note:
- This method is intended to be invoked by the task scope owner after it
has invoked
join
(orjoinUntil
). A future release may add enforcement to prevent the method being called by other threads or before joining. - Type Parameters:
X
- type of the exception to be thrown- Parameters:
esf
- the exception supplying function- Returns:
- the result of the first subtask that completed with a result
- Throws:
X
- if no subtask completed with a resultIllegalStateException
- if the handle method was not invoked with a completed subtask
-