: Re: The Great Concurrency Smackdown: ZIO versus JDK by John A. De Goes
Arnaud Masson
arnaud.masson at fr.ibm.com
Wed Mar 15 17:38:13 UTC 2023
Nice, thanks.
I think this is especially useful when we must call a “blackbox library” from finally/cleanup block (so we can’t control if the special uninterruptible versions of APIs – if they exist - are used inside).
Arnaud
De : Ron Pressler <ron.pressler at oracle.com>
Date : mercredi, 15 mars 2023 à 16:27
À : Arnaud Masson <arnaud.masson at fr.ibm.com>
Cc : loom-dev at openjdk.org <loom-dev at openjdk.org>, Piotr Tarsa <piotr.tarsa at gmail.com>
Objet : Re: [External] : Re: The Great Concurrency Smackdown: ZIO versus JDK by John A. De Goes
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
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.
ZjQcmQRYFpfptBannerEnd
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://github.com/google/guava/issues/1409#issuecomment-389469315>
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
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/9df12984/attachment-0001.htm>
More information about the loom-dev
mailing list