Module jdk.incubator.concurrent
Package jdk.incubator.concurrent
Class StructuredTaskScope.ShutdownOnFailure
java.lang.Object
jdk.incubator.concurrent.StructuredTaskScope<Object>
jdk.incubator.concurrent.StructuredTaskScope.ShutdownOnFailure
- All Implemented Interfaces:
AutoCloseable
- Enclosing class:
StructuredTaskScope<T>
A
StructuredTaskScope that captures the exception of the first subtask to
complete abnormally. 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 results for all subtasks are required
("invoke all"); if any subtask fails then the results of other unfinished subtasks
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
ConstructorsConstructorDescriptionConstructs a new unnamedShutdownOnFailurethat creates virtual threads.ShutdownOnFailure(String name, ThreadFactory factory) Constructs a newShutdownOnFailurewith the given name and thread factory. -
Method Summary
Modifier and TypeMethodDescriptionReturns the exception for the first subtask that completed with an exception.protected voidhandleComplete(Future<Object> future) Shut down the given task scope when invoked for the first time with aFuturefor a task that completed abnormally (exception or cancelled).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.voidThrows if a subtask completed abnormally.<X extends Throwable>
voidthrowIfFailed(Function<Throwable, ? extends X> esf) Throws the exception produced by the given exception supplying function if a subtask completed abnormally.Methods declared in class jdk.incubator.concurrent.StructuredTaskScope
close, fork, shutdown
-
Constructor Details
-
ShutdownOnFailure
Constructs a newShutdownOnFailurewith the given name and thread factory. The task scope is optionally named for the purposes of monitoring and management. The thread factory is used tocreatethreads when tasks are forked. The task scope is owned by the current thread.- Parameters:
name- the name of the task scope, can be nullfactory- the thread factory
-
ShutdownOnFailure
public ShutdownOnFailure()Constructs a new unnamedShutdownOnFailurethat creates virtual threads.This constructor is equivalent to invoking the 2-arg constructor with a name of
nulland a thread factory that creates virtual threads.
-
-
Method Details
-
handleComplete
Shut down the given task scope when invoked for the first time with aFuturefor a task that completed abnormally (exception or cancelled).- Overrides:
handleCompletein classStructuredTaskScope<Object>- 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 andhandleCompletemethod), theshutdownmethod 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:
joinin classStructuredTaskScope<Object>- 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.ShutdownOnFailure 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 andhandleCompletemethod), theshutdownmethod 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:
joinUntilin classStructuredTaskScope<Object>- 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
-
exception
Returns the exception for the first subtask that completed with an exception. If no subtask completed with an exception but cancelled subtasks were notified to thehandleCompletemethod then aCancellationExceptionis returned. If no subtasks completed abnormally then an emptyOptionalis returned.- 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 exception for a subtask that completed abnormally or an empty optional if no subtasks completed abnormally
-
throwIfFailed
Throws if a subtask completed abnormally. If any subtask completed with an exception thenExecutionExceptionis thrown with the exception of the first subtask to fail as the cause. If no subtask completed with an exception but cancelled subtasks were notified to thehandleCompletemethod thenCancellationExceptionis thrown. This method does nothing if no subtasks completed abnormally.- 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. - Throws:
ExecutionException- if a subtask completed with an exceptionCancellationException- if no subtasks completed with an exception but subtasks were cancelled
-
throwIfFailed
Throws the exception produced by the given exception supplying function if a subtask completed abnormally. If any subtask completed with an exception then the function is invoked with the exception of the first subtask to fail. If no subtask completed with an exception but cancelled subtasks were notified to thehandleCompletemethod then the function is called with aCancellationException. The exception returned by the function is thrown. This method does nothing if no subtasks completed abnormally.- 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- Throws:
X- produced by the exception supplying function
-