[External] : Re: The Great Concurrency Smackdown: ZIO versus JDK by John A. De Goes

Ron Pressler ron.pressler at oracle.com
Wed Mar 15 15:26:57 UTC 2023


If you really want to run some code uninterruptibly, you’d either use an uninterruptible version of the method (which is available for some methods in the JDK), or protect the code from interruption by submitting it as a task and controlling it with a future. Here’s how you can implement a general `doUninterruptibly` in a similar way, only using StructuredTaskScope:

<T> T doUninterruptibly(Callable<T> task) throws ExecutionException {
    boolean interrupted = false;
    try (var s = new StructuredTaskScope.ShutdownOnSuccess<T>()) {
        s.fork(task);
        while (true) {
            try {
                return s.join().result();
            } catch (InterruptedException ex) {
                interrupted = true;
            }
        }
    } finally {
        if (interrupted) Thread.currentThread().interrupt();
    }
}

On 15 Mar 2023, at 14:37, Arnaud Masson <arnaud.masson at fr.ibm.com<mailto:arnaud.masson at fr.ibm.com>> wrote:

Yes, that avoids problems most of the time,
but what if closeStuff() could throw InterruptedException?
(If the thread interruption request occurs during the finally block, not during the try block.)

thanks
Arnaud

On 15 Mar 2023, at 14:15, Arnaud Masson <arnaud.masson at fr.ibm.com<mailto:arnaud.masson at fr.ibm.com>> wrote:

Yes, that would be nice for finally { } blocks.
A bit like what’s suggested here: https://github.com/google/guava/issues/1409#issuecomment-389469315<https://urldefense.com/v3/__https://github.com/google/guava/issues/1409*issuecomment-389469315__;Iw!!ACWV5N9M2RV99hQ!LPqV7AcWfT6m_7dfb8zJEShCUVeBOeHaNQ3vvGyWTLw6-zr7MFlMDAB_Iwdm5l7rxkvmdUjY5Gzo8W__L_H-hfPHZg$>

try {
   doSomeIO();  // can throw InterruptedException
   …
} finally {
   Uninterruptibles.runUninterruptibly(() ->
      closeStuff() // cannot throw InterruptedException
   ) // auto rethrow (delayed) InterruptedException  if needed
}

What about try-with-resource?

thanks
Arnaud



Methods that throw InterruptedException clear the interrupt status precisely to allow calling interruptible methods in cleanup code.
Unless otherwise stated above:

Compagnie IBM France
Siège Social : 17, avenue de l'Europe, 92275 Bois-Colombes Cedex
RCS Nanterre 552 118 465
Forme Sociale : S.A.S.
Capital Social : 664 069 390,60 €
SIRET : 552 118 465 03644 - Code NAF 6203Z

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20230315/b131b773/attachment.htm>


More information about the loom-dev mailing list