Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Take heed of this innocuous line from the <code>ScheduledExecutorService.schedule...</code> <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html" rel="nofollow">Javadoc</a> </p> <p><em>If any execution of the task encounters an exception, subsequent executions are suppressed.</em></p> <p>This means that if you are running into an <code>Exception</code> at some point and not handling it, the <code>Exception</code> will propagate into the <code>ScheduledExecutorService</code> and it will kill your task.</p> <p>To avoid this problem you need to make sure the <em>entire</em> <code>Runnable</code> is wrapped in a <code>try...catch</code> and <code>Exception</code>s are <em>guaranteed</em> to never be unhandled.</p> <p>You can also extend the <code>ScheduledExecutorService</code> (also mentioned in the <a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html#afterExecute%28java.lang.Runnable,%20java.lang.Throwable%29" rel="nofollow">javadoc</a>) to handle uncaught exceptions :-</p> <pre><code>final ScheduledExecutorService ses = new ScheduledThreadPoolExecutor(10){ @Override protected void afterExecute(Runnable r, Throwable t) { super.afterExecute(r, t); if (t == null &amp;&amp; r instanceof Future&lt;?&gt;) { try { Object result = ((Future&lt;?&gt;) r).get(); } catch (CancellationException ce) { t = ce; } catch (ExecutionException ee) { t = ee.getCause(); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); // ignore/reset } } if (t != null) { System.out.println(t); } } }; </code></pre> <p>Here the <code>afterExecute</code> method simply <code>System.out.println</code>s the <code>Throwable</code> but it could do other things. Alert users, restart tasks etc...</p>
 

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