<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
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:
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">
<div class="">
<div class=""><font face="Courier New" class=""><T> T doUninterruptibly(Callable<T> task) throws ExecutionException {</font></div>
</div>
<font face="Courier New" class="">    boolean interrupted = false;</font>
<div class="">
<div class=""><font face="Courier New" class="">    try (var s = new StructuredTaskScope.ShutdownOnSuccess<T>()) {</font></div>
</div>
<div class="">
<div class=""><font face="Courier New" class="">        s.fork(task);</font></div>
</div>
<div class="">
<div class=""><font face="Courier New" class="">        while (true) {</font></div>
</div>
<div class="">
<div class=""><font face="Courier New" class="">            try {</font></div>
</div>
<div class="">
<div class=""><font face="Courier New" class="">                return s.join().result();</font></div>
</div>
<div class="">
<div class=""><font face="Courier New" class="">            } catch (InterruptedException ex) {</font></div>
<div class=""><font face="Courier New" class="">                interrupted = true;</font></div>
<div class=""><font face="Courier New" class="">            }</font></div>
</div>
<div class="">
<div class=""><font face="Courier New" class="">        }</font></div>
</div>
<div class="">
<div class=""><font face="Courier New" class="">    } finally {</font></div>
</div>
<font face="Courier New" class="">        if (interrupted) Thread.currentThread().interrupt();</font>
<div class=""><font face="Courier New" class="">    }</font></div>
<div class="">
<div class=""><font face="Courier New" class="">}</font></div>
</div>
</blockquote>
<div class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On 15 Mar 2023, at 14:37, Arnaud Masson <<a href="mailto:arnaud.masson@fr.ibm.com" class="">arnaud.masson@fr.ibm.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">Yes, that avoids problems most of the time,<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">but what if closeStuff() could throw<span class="Apple-converted-space"> </span></span><span lang="EN-US" class="">InterruptedException?<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">(If the thread interruption request occurs during the finally block, not during the try block.)<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class=""><br class="">
thanks<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">Arnaud<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class=""><o:p class=""> </o:p></span></div>
<blockquote style="margin-top: 5pt; margin-bottom: 5pt;" class="">
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
On 15 Mar 2023, at 14:15, Arnaud Masson <<a href="mailto:arnaud.masson@fr.ibm.com" style="color: blue; text-decoration: underline;" class="">arnaud.masson@fr.ibm.com</a>> wrote:<o:p class=""></o:p></div>
</div>
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div class="">
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">Yes, that would be nice for finally { } blocks.</span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">A bit like what’s suggested here:<span class="apple-converted-space"> </span><a href="https://urldefense.com/v3/__https://github.com/google/guava/issues/1409*issuecomment-389469315__;Iw!!ACWV5N9M2RV99hQ!LPqV7AcWfT6m_7dfb8zJEShCUVeBOeHaNQ3vvGyWTLw6-zr7MFlMDAB_Iwdm5l7rxkvmdUjY5Gzo8W__L_H-hfPHZg$" style="color: blue; text-decoration: underline;" class=""><span style="color: rgb(5, 99, 193);" class="">https://github.com/google/guava/issues/1409#issuecomment-389469315</span></a></span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class=""> </span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">try {</span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">   doSomeIO();  // can throw InterruptedException</span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">   …</span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">} finally {</span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">   Uninterruptibles.runUninterruptibly(() -></span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">      closeStuff() // cannot throw InterruptedException</span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">   ) // auto rethrow (delayed) InterruptedException  if needed</span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">}</span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class=""> </span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class="">What about try-with-resource?</span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" class=""> </span><span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
thanks<span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Arnaud<span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
 <span style="font-size: 10pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 10pt;" class=""><o:p class=""> </o:p></span></div>
</div>
</div>
</blockquote>
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0cm 0cm 0cm 35.4pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Methods that throw InterruptedException clear the interrupt status precisely to allow calling interruptible methods in cleanup code.<o:p class=""></o:p></div>
</div>
</div>
<div style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class="">
Unless otherwise stated above:<br class="">
<br class="">
Compagnie IBM France<br class="">
Siège Social : 17, avenue de l'Europe, 92275 Bois-Colombes Cedex<br class="">
RCS Nanterre 552 118 465<br class="">
Forme Sociale : S.A.S.<br class="">
Capital Social : 664 069 390,60 €<br class="">
SIRET : 552 118 465 03644 - Code NAF 6203Z</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>