Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to (reliably) interrupt threads form the main thread after a specific amount of time in Java?
    text
    copied!<p>I just started out with threading. I wrote a main class that sets up and starts 100 threads, waits 5 seconds and then interrupts them (at least that's what I thought it did):</p> <pre><code>public static void main(String[] args) { List&lt;Thread&gt; threads = new ArrayList&lt;Thread&gt;(); for (int i = 0; i &lt; 100; i++) { Thread t = new Thread(new Walker()); threads.add(t); } System.out.println("Starting threads..."); for (Thread thread : threads) { thread.start(); } try { Thread.sleep(5000); } catch (InterruptedException e) { // don't do anything } System.out.println("Time's up - Terminating threads..."); for (Thread t : threads) { t.interrupt(); System.out.print("."); } for (Thread t : threads) { try { t.join(10); } catch (InterruptedException e) { // don't do anything } } System.out.println(""); System.out.println("All done."); } </code></pre> <p>The threads looked a bit like this:</p> <pre><code>public class Walker implements Runnable { public void run() { for (int i = 0;; i++) { //do some complicated stuff that takes some time System.out.println(Thread.currentThread().getName() + ":" + i); if (Thread.interrupted()) { break; } } } } </code></pre> <p>Now, the output I got was that the main thread began interrupting threads, but some sub threads continued to run a few times (i.e. loop iterations) before terminating, e.g.</p> <blockquote> <p>Starting threads...<br/> Thread-1:0<br/> Thread-2:0<br/> Thread-1:1<br/> Thread-3:0<br/> [...]<br/> Time's up - Terminating threads...<br/> ......Thread-1:60<br/> Thread-1:61<br/> ...Thread-1:62<br/> Thread-2:55<br/> ..All done.<br/> [output from threads sometimes continued even here - after the join()]</p> </blockquote> <p>At that time I didn't fully understand that a single thread could be allocated enough processor time to run a few times - I expected at most one additional run before the main thread had the opportunity to interrupt it.</p> <p>But while I now see that it is absolutely fine for a thread to be executed for some (long) time before the main thread gets a chance to terminate (i.e. interrupt) it, I am still wondering: is there an <strong><em>easy</em></strong> way to interrupt all child threads in a timely manner from the main thread? (Setting a "time to live" through a thread's constructor and then testing inside the Walker class for it is not what I want.)</p> <p>Also: is it possible for the last print statement to execute and then see some output from individual threads - after all threads were join()ed? (Maybe I have a glitch somewhere else; the actual code is a bit more complex...)</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