Skip to content

Commit 3661cde

Browse files
author
Alan Bateman
committedJun 22, 2023
8309853: StructuredTaskScope.join description improvements
Reviewed-by: rpressler, darcy
1 parent ac44ef1 commit 3661cde

File tree

1 file changed

+79
-28
lines changed

1 file changed

+79
-28
lines changed
 

‎src/java.base/share/classes/java/util/concurrent/StructuredTaskScope.java

+79-28
Original file line numberDiff line numberDiff line change
@@ -625,10 +625,13 @@ private void implJoin(Duration timeout)
625625
}
626626

627627
/**
628-
* Wait for all threads in this task scope to finish or the task scope to shut down.
629-
* This method waits until all threads started in this task scope finish execution,
630-
* the {@link #shutdown() shutdown} method is invoked to shut down the task scope,
631-
* or the current thread is {@linkplain Thread#interrupt() interrupted}.
628+
* Wait for all subtasks started in this task scope to finish or the task scope to
629+
* shut down.
630+
*
631+
* <p> This method waits for all subtasks by waiting for all threads {@linkplain
632+
* #fork(Callable) started} in this task scope to finish execution. It stops waiting
633+
* when all threads finish, the task scope is {@linkplain #shutdown() shut down}, or
634+
* the current thread is {@linkplain Thread#interrupt() interrupted}.
632635
*
633636
* <p> This method may only be invoked by the task scope owner.
634637
*
@@ -652,11 +655,14 @@ public StructuredTaskScope<T> join() throws InterruptedException {
652655
}
653656

654657
/**
655-
* Wait for all threads in this task scope to finish or the task scope to shut down,
656-
* up to the given deadline. This method waits until all threads started in the task
657-
* scope finish execution, the {@link #shutdown() shutdown} method is invoked to
658-
* shut down the task scope, the current thread is {@linkplain Thread#interrupt()
659-
* interrupted}, or the deadline is reached.
658+
* Wait for all subtasks started in this task scope to finish or the task scope to
659+
* shut down, up to the given deadline.
660+
*
661+
* <p> This method waits for all subtasks by waiting for all threads {@linkplain
662+
* #fork(Callable) started} in this task scope to finish execution. It stops waiting
663+
* when all threads finish, the task scope is {@linkplain #shutdown() shut down}, the
664+
* deadline is reached, or the current thread is {@linkplain Thread#interrupt()
665+
* interrupted}.
660666
*
661667
* <p> This method may only be invoked by the task scope owner.
662668
*
@@ -950,21 +956,25 @@ public String toString() {
950956
yield "[Failed: " + ex + "]";
951957
}
952958
};
953-
return Objects.toIdentityString(this ) + stateAsString;
959+
return Objects.toIdentityString(this) + stateAsString;
954960
}
955961
}
956962

957963
/**
958964
* A {@code StructuredTaskScope} that captures the result of the first subtask to
959965
* complete {@linkplain Subtask.State#SUCCESS successfully}. Once captured, it
960-
* invokes the {@linkplain #shutdown() shutdown} method to interrupt unfinished threads
966+
* {@linkplain #shutdown() shuts down} the task scope to interrupt unfinished threads
961967
* and wakeup the task scope owner. The policy implemented by this class is intended
962968
* for cases where the result of any subtask will do ("invoke any") and where the
963969
* results of other unfinished subtasks are no longer needed.
964970
*
965971
* <p> Unless otherwise specified, passing a {@code null} argument to a method
966972
* in this class will cause a {@link NullPointerException} to be thrown.
967973
*
974+
* @apiNote This class implements a policy to shut down the task scope when a subtask
975+
* completes successfully. There shouldn't be any need to directly shut down the task
976+
* scope with the {@link #shutdown() shutdown} method.
977+
*
968978
* @param <T> the result type
969979
* @since 21
970980
*/
@@ -1017,8 +1027,6 @@ public ShutdownOnSuccess() {
10171027

10181028
@Override
10191029
protected void handleComplete(Subtask<? extends T> subtask) {
1020-
super.handleComplete(subtask);
1021-
10221030
if (firstResult != null) {
10231031
// already captured a result
10241032
return;
@@ -1038,8 +1046,18 @@ protected void handleComplete(Subtask<? extends T> subtask) {
10381046
}
10391047

10401048
/**
1041-
* {@inheritDoc}
1042-
* @return this task scope
1049+
* Wait for a subtask started in this task scope to complete {@linkplain
1050+
* Subtask.State#SUCCESS successfully} or all subtasks to complete.
1051+
*
1052+
* <p> This method waits for all subtasks by waiting for all threads {@linkplain
1053+
* #fork(Callable) started} in this task scope to finish execution. It stops waiting
1054+
* when all threads finish, a subtask completes successfully, or the current
1055+
* thread is {@linkplain Thread#interrupt() interrupted}. It also stops waiting
1056+
* if the {@link #shutdown() shutdown} method is invoked directly to shut down
1057+
* this task scope.
1058+
*
1059+
* <p> This method may only be invoked by the task scope owner.
1060+
*
10431061
* @throws IllegalStateException {@inheritDoc}
10441062
* @throws WrongThreadException {@inheritDoc}
10451063
*/
@@ -1050,8 +1068,19 @@ public ShutdownOnSuccess<T> join() throws InterruptedException {
10501068
}
10511069

10521070
/**
1053-
* {@inheritDoc}
1054-
* @return this task scope
1071+
* Wait for a subtask started in this task scope to complete {@linkplain
1072+
* Subtask.State#SUCCESS successfully} or all subtasks to complete, up to the
1073+
* given deadline.
1074+
*
1075+
* <p> This method waits for all subtasks by waiting for all threads {@linkplain
1076+
* #fork(Callable) started} in this task scope to finish execution. It stops waiting
1077+
* when all threads finish, a subtask completes successfully, the deadline is
1078+
* reached, or the current thread is {@linkplain Thread#interrupt() interrupted}.
1079+
* It also stops waiting if the {@link #shutdown() shutdown} method is invoked
1080+
* directly to shut down this task scope.
1081+
*
1082+
* <p> This method may only be invoked by the task scope owner.
1083+
*
10551084
* @throws IllegalStateException {@inheritDoc}
10561085
* @throws WrongThreadException {@inheritDoc}
10571086
*/
@@ -1073,8 +1102,8 @@ public ShutdownOnSuccess<T> joinUntil(Instant deadline)
10731102
*
10741103
* @throws ExecutionException if no subtasks completed successfully but at least
10751104
* one subtask failed
1076-
* @throws IllegalStateException if the handleComplete method was not invoked with
1077-
* a completed subtask, or the task scope owner did not join after forking
1105+
* @throws IllegalStateException if no subtasks completed or the task scope owner
1106+
* did not join after forking
10781107
* @throws WrongThreadException if the current thread is not the task scope owner
10791108
*/
10801109
public T result() throws ExecutionException {
@@ -1095,8 +1124,8 @@ public T result() throws ExecutionException {
10951124
* @return the result of the first subtask that completed with a result
10961125
*
10971126
* @throws X if no subtasks completed successfully but at least one subtask failed
1098-
* @throws IllegalStateException if the handleComplete method was not invoked with
1099-
* a completed subtask, or the task scope owner did not join after forking
1127+
* @throws IllegalStateException if no subtasks completed or the task scope owner
1128+
* did not join after forking
11001129
* @throws WrongThreadException if the current thread is not the task scope owner
11011130
*/
11021131
public <X extends Throwable> T result(Function<Throwable, ? extends X> esf) throws X {
@@ -1125,15 +1154,19 @@ public <X extends Throwable> T result(Function<Throwable, ? extends X> esf) thro
11251154

11261155
/**
11271156
* A {@code StructuredTaskScope} that captures the exception of the first subtask to
1128-
* {@linkplain Subtask.State#FAILED fail}. Once captured, it invokes the {@linkplain
1129-
* #shutdown() shutdown} method to interrupt unfinished threads and wakeup the task
1157+
* {@linkplain Subtask.State#FAILED fail}. Once captured, it {@linkplain #shutdown()
1158+
* shuts down} the task scope to interrupt unfinished threads and wakeup the task
11301159
* scope owner. The policy implemented by this class is intended for cases where the
11311160
* results for all subtasks are required ("invoke all"); if any subtask fails then the
11321161
* results of other unfinished subtasks are no longer needed.
11331162
*
11341163
* <p> Unless otherwise specified, passing a {@code null} argument to a method
11351164
* in this class will cause a {@link NullPointerException} to be thrown.
11361165
*
1166+
* @apiNote This class implements a policy to shut down the task scope when a subtask
1167+
* fails. There shouldn't be any need to directly shut down the task scope with the
1168+
* {@link #shutdown() shutdown} method.
1169+
*
11371170
* @since 21
11381171
*/
11391172
@PreviewFeature(feature = PreviewFeature.Feature.STRUCTURED_CONCURRENCY)
@@ -1181,7 +1214,6 @@ public ShutdownOnFailure() {
11811214

11821215
@Override
11831216
protected void handleComplete(Subtask<?> subtask) {
1184-
super.handleComplete(subtask);
11851217
if (subtask.state() == Subtask.State.FAILED
11861218
&& firstException == null
11871219
&& FIRST_EXCEPTION.compareAndSet(this, null, subtask.exception())) {
@@ -1190,8 +1222,17 @@ protected void handleComplete(Subtask<?> subtask) {
11901222
}
11911223

11921224
/**
1193-
* {@inheritDoc}
1194-
* @return this task scope
1225+
* Wait for all subtasks started in this task scope to complete or for a subtask
1226+
* to {@linkplain Subtask.State#FAILED fail}.
1227+
*
1228+
* <p> This method waits for all subtasks by waiting for all threads {@linkplain
1229+
* #fork(Callable) started} in this task scope to finish execution. It stops waiting
1230+
* when all threads finish, a subtask fails, or the current thread is {@linkplain
1231+
* Thread#interrupt() interrupted}. It also stops waiting if the {@link #shutdown()
1232+
* shutdown} method is invoked directly to shut down this task scope.
1233+
*
1234+
* <p> This method may only be invoked by the task scope owner.
1235+
*
11951236
* @throws IllegalStateException {@inheritDoc}
11961237
* @throws WrongThreadException {@inheritDoc}
11971238
*/
@@ -1202,8 +1243,18 @@ public ShutdownOnFailure join() throws InterruptedException {
12021243
}
12031244

12041245
/**
1205-
* {@inheritDoc}
1206-
* @return this task scope
1246+
* Wait for all subtasks started in this task scope to complete or for a subtask
1247+
* to {@linkplain Subtask.State#FAILED fail}, up to the given deadline.
1248+
*
1249+
* <p> This method waits for all subtasks by waiting for all threads {@linkplain
1250+
* #fork(Callable) started} in this task scope to finish execution. It stops waiting
1251+
* when all threads finish, a subtask fails, the deadline is reached, or the current
1252+
* thread is {@linkplain Thread#interrupt() interrupted}. It also stops waiting
1253+
* if the {@link #shutdown() shutdown} method is invoked directly to shut down
1254+
* this task scope.
1255+
*
1256+
* <p> This method may only be invoked by the task scope owner.
1257+
*
12071258
* @throws IllegalStateException {@inheritDoc}
12081259
* @throws WrongThreadException {@inheritDoc}
12091260
*/

0 commit comments

Comments
 (0)
Please sign in to comment.