AutoCloseable
StructuredTaskScopePREVIEW<T>
public static final class StructuredTaskScope.ShutdownOnFailure extends StructuredTaskScopePREVIEW<Object>
ShutdownOnFailure
is a preview API of the Java platform. StructuredTaskScope
that captures the exception of the first subtask to failPREVIEW. Once captured, it shuts downPREVIEW the task scope to interrupt unfinished threads and wakeup the task scope 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.
StructuredTaskScope.ShutdownOnFailurePREVIEW, StructuredTaskScope.ShutdownOnSuccessPREVIEW<T>, StructuredTaskScope.SubtaskPREVIEW<T>
Constructor | Description |
---|---|
ShutdownOnFailure() |
Constructs a new unnamed ShutdownOnFailure that creates virtual threads. |
ShutdownOnFailure |
Constructs a new ShutdownOnFailure with the given name and thread factory. |
Modifier and Type | Method | Description |
---|---|---|
Optional |
exception() |
|
StructuredTaskScope.ShutdownOnFailurePREVIEW |
join() |
|
StructuredTaskScope.ShutdownOnFailurePREVIEW |
joinUntil |
|
void |
throwIfFailed() |
|
<X extends Throwable> |
throwIfFailed |
close, ensureOwnerAndJoined, fork, handleComplete, isShutdown, shutdown
public ShutdownOnFailure(String name, ThreadFactory factory)
ShutdownOnFailure
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 to create
threads when subtasks are forkedPREVIEW. The task scope is owned by the current thread. Construction captures the current thread's scoped valuePREVIEW bindings for inheritance by threads started 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.
name
- the name of the task scope, can be nullfactory
- the thread factorypublic ShutdownOnFailure()
ShutdownOnFailure
that creates virtual threads.null
and a thread factory that creates virtual threads.public StructuredTaskScope.ShutdownOnFailurePREVIEW join() throws InterruptedException
This method waits for all subtasks by waiting for all threads startedPREVIEW in this task scope to finish execution. It stops waiting when all threads finish, a subtask fails, or the current thread is interrupted. It also stops waiting if the shutdown
PREVIEW method is invoked directly to shut down this task scope.
This method may only be invoked by the task scope owner.
join
in class StructuredTaskScopePREVIEW<Object>
IllegalStateException
- if this task scope is closedWrongThreadException
- if the current thread is not the task scope ownerInterruptedException
- if interrupted while waitingpublic StructuredTaskScope.ShutdownOnFailurePREVIEW joinUntil(Instant deadline) throws InterruptedException, TimeoutException
This method waits for all subtasks by waiting for all threads startedPREVIEW in this task scope to finish execution. It stops waiting when all threads finish, a subtask fails, the deadline is reached, or the current thread is interrupted. It also stops waiting if the shutdown
PREVIEW method is invoked directly to shut down this task scope.
This method may only be invoked by the task scope owner.
joinUntil
in class StructuredTaskScopePREVIEW<Object>
deadline
- the deadlineIllegalStateException
- if this task scope is closedWrongThreadException
- if the current thread is not the task scope ownerInterruptedException
- if interrupted while waitingTimeoutException
- if the deadline is reached while waitingpublic Optional<Throwable> exception()
Optional
is returned.WrongThreadException
- if the current thread is not the task scope ownerIllegalStateException
- if the task scope owner did not join after forkingpublic void throwIfFailed() throws ExecutionException
ExecutionException
is thrown with the exception of the first subtask to fail as the cause. This method does nothing if no subtasks failed.ExecutionException
- if a subtask failedWrongThreadException
- if the current thread is not the task scope ownerIllegalStateException
- if the task scope owner did not join after forkingpublic <X extends Throwable> void throwIfFailed(Function<Throwable,? extends X> esf) throws X
X
- type of the exception to be thrownesf
- the exception supplying functionX
- produced by the exception supplying functionWrongThreadException
- if the current thread is not the task scope ownerIllegalStateException
- if the task scope owner did not join after forking
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/concurrent/StructuredTaskScope.ShutdownOnFailure.html
ShutdownOnFailure
when preview features are enabled.