Unclear on close()

eric at kolotyluk.net eric at kolotyluk.net
Wed Jul 27 16:35:15 UTC 2022


>From https://openjdk.org/jeps/428 and
https://download.java.net/java/early_access/loom/docs/api/jdk.incubator.conc
urrent/jdk/incubator/concurrent/StructuredTaskScope.html

 

public void close()

Closes this task scope. 

This method first shuts down the task scope (as if by invoking the
<https://download.java.net/java/early_access/loom/docs/api/jdk.incubator.con
current/jdk/incubator/concurrent/StructuredTaskScope.html#shutdown()>
shutdown method). It then waits for the threads executing any unfinished
tasks to finish. If interrupted then this method will continue to wait for
the threads to finish before completing with the interrupt status set. 

This method may only be invoked by the task scope owner. 

A StructuredTaskScope is intended to be used in a structured manner. If this
method is called to close a task scope before nested task scopes are closed
then it closes the underlying construct of each nested task scope (in the
reverse order that they were created in), closes this task scope, and then
throws
<https://download.java.net/java/early_access/loom/docs/api/jdk.incubator.con
current/jdk/incubator/concurrent/StructureViolationException.html>
StructureViolationException. Similarly, if called to close a task scope that
encloses operations
<https://download.java.net/java/early_access/loom/docs/api/jdk.incubator.con
current/jdk/incubator/concurrent/ExtentLocal.Carrier.html#run(java.lang.Runn
able)>  with extent-local bindings then it also throws
StructureViolationException after closing the task scope.

I am unclear on "It then waits for the threads executing any unfinished
tasks to finish."

 

   Instant deadline = ...

 

   try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {

 

        Future<String> future1 = scope.fork(() -> query(left));

        Future<String> future2 = scope.fork(() -> query(right));

 

        scope.joinUntil(deadline);

 

        scope.
<https://download.java.net/java/early_access/loom/docs/api/jdk.incubator.con
current/jdk/incubator/concurrent/StructuredTaskScope.ShutdownOnFailure.html#
throwIfFailed()> throwIfFailed(e -> new WebApplicationException(e));

 

        // both tasks completed successfully

        String result = Stream.of(future1, future2)

                .map(
<https://download.java.net/java/early_access/loom/docs/api/java.base/java/ut
il/concurrent/Future.html#resultNow()> Future::resultNow)

                .collect(Collectors.joining(", ", "{ ", " }"));

 

        ...

    }

 

1.	Is there any scenario where close() waits forever?

a.	Where it is implicit in this try block.
b.	I can imagine scenarios where subtasks don't cancel properly or
respond correctly to interrupts.

2.	If there is, is there any programmatic way out of this?

a.	Does the InterruptedException bypass close() and exit the try block?
b.	Is this guaranteed by the runtime?
c.	I assume it is, but I have made bad assumptions about the runtime
before.

3.	Personally, I would have thought that "scope.joinUntil(deadline);"
would guarantee this code exits the try block, but the documentation, as
written, does not give me that confidence. There are two wait points.

a.	scope.joinUntil(deadline);
b.	scope.close(); 

4.	While this may not be ambiguous to others, it is to me.

a.	It would be nice if there was text that made this more explicit.

 

Cheers, Eric

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20220727/03f25cd9/attachment-0001.htm>


More information about the loom-dev mailing list