Question about StructuredTaskScope.joinUntil(Instant)

Sten Nordstrom stnordstrom at gmail.com
Sun Jul 28 13:10:34 UTC 2024


This was surprising to me as well, as this can lead to some surprising
results in environments with unreliable time synchronization. Are there any
pointers re discussion why this approach was chosen instead of the more
common way of specifying a maximum duration to wait?

Glad to hear that you found a solution, we will need to do something
similar when we start using StructuredTaskScopes.

Best Regards,
Sten



On Tue, Jun 11, 2024 at 18.33 Werner Randelshofer <
werner.randelshofer at fibermail.ch> wrote:

> I have solved my problem. :-)
>
> I have now implemented a subclass of StructuredTaskScope, which allows to
> specify a deadline in System.nanoTime() units.
> With this design, the deadline is independent from external resources
> (like NTP time servers) over which the Java process has no control.
>
> See code below.
>
> With best regards,
> Werner
>
>
>
> package org.example.structuredconcurrency;
>
> import java.time.Duration;
> import java.util.concurrent.StructuredTaskScope;
> import java.util.concurrent.ThreadFactory;
> import java.util.concurrent.TimeoutException;
>
> /**
>  * A {@link StructuredTaskScope} that supports a deadline given in {@link
> System#nanoTime()}.
>  *
>  * @param <T> the type of the structured task scope
>  */
> public class StructuredTaskScopeNanoTime<T> extends StructuredTaskScope<T>
> {
>
>     /**
>      * Creates an unnamed structured task scope that creates virtual
> threads. The task
>      * scope is owned by the current thread.
>      */
>     public StructuredTaskScopeNanoTime() {
>     }
>
>     /**
>      * Creates a structured task scope with the given name and thread
> factory.
>      *
>      * @param name    the name of the task scope, can be null
>      * @param factory the thread factory
>      */
>     public StructuredTaskScopeNanoTime(String name, ThreadFactory factory)
> {
>         super(name, factory);
>     }
>
>     /**
>      * Wait for all subtasks started in this task scope to finish or the
> task scope to
>      * shut down, up to the given deadline.
>      *
>      * @param deadlineNanoTime the deadline given in {@link
> System#nanoTime()} units
>      * @return this task scope
>      * @throws IllegalStateException if this task scope is closed
>      * @throws WrongThreadException  if the current thread is not the task
> scope owner
>      * @throws InterruptedException  if interrupted while waiting
>      * @throws TimeoutException      if the deadline is reached while
> waiting
>      */
>     public StructuredTaskScopeNanoTime<T> joinUntilNanoTime(long
> deadlineNanoTime) throws InterruptedException, TimeoutException {
>         fork(() -> {
>             Thread.sleep(Duration.ofNanos(deadlineNanoTime -
> System.nanoTime()));
>             shutdown();
>             return null;
>         });
>         join();
>         return this;
>     }
> }
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20240728/f4971cba/attachment-0001.htm>


More information about the loom-dev mailing list