Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are catching <code>InterruptedException</code> anywhere inside the for-loop, remove all of those try/catch blocks and instead have a single try/catch which surrounds the entire for-loop. This will allow the entire for-loop to cease when you interrupt its thread.</p> <p>Similarly, if you are catching <code>IOException</code>, catch <code>InterruptedIOException</code> and <code>ClosedByInterruptException</code> first. Move those catch blocks outside of the for-loop. (The compiler won't allow it if you are catching <code>IOException</code> internally, since there would be nothing to catch at the outer level.)</p> <p>If the blocking calls are not throwing <code>InterruptedException</code>, you will need to add a check after each of them, like this:</p> <pre><code>if (Thread.interrupted()) { break; } </code></pre> <p>If you have many levels of loops, you might want to add a label so you can directly exit from that first "instructions" loop, without the need for adding lots of flag variables:</p> <pre><code>instructions: for ( ... ) { for ( ... ) { doLongOperation(); if (Thread.interrupted()) { break instructions; } } } </code></pre> <p>Either way, once interrupts are handled, you can have a background thread interrupt your first for-loop:</p> <pre><code>final Thread instructionsThread = Thread.currentThread(); Runnable interruptor = new Runnable() { public void run() { instructionsThread.interrupt(); } }; ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); executor.schedule(interruptor, 5, TimeUnit.MINUTES); // instructions try { for ( ... ) { } } catch (InterruptedException | InterruptedIOException | ClosedByInterruptException e) { logger.log(Level.FINE, "First loop timed out.", e); } finally { executor.shutdown(); } // instructions2 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload