Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem you observe is probably due to how <code>System.out.println</code> works. It is a synchronized method. So a likely explanation is:</p> <ul> <li>when calling <code>System.out.print(".");</code> after <code>t.interrupt();</code>, your main thread acquires the lock to print</li> <li>before the lock is released, worker threads arrive at <code>System.out.println(Thread.currentThread().getName() + ":" + i);</code> and wait for the lock</li> <li>when the main thread releases the lock, all the worker threads that were waiting print their progress.</li> <li>the main thread arrives at <code>System.out.print(".");</code> again and has to wait for the print lock to be available, etc.</li> </ul> <p>Regarding the fact that you see more prints from the worker threads after <code>"All Done"</code> is printed: you only <code>join</code> for 10 ms, so it is possible that it is not enough and a thread is not finished within 10ms of being interrupted. If you just use <code>join()</code> you should not see that any longer.</p> <p>Example of Worker class that reproduces the behaviour you observe:</p> <pre><code>class Walker implements Runnable { public void run() { for (int i = 0;; i++) { try { Thread.sleep(100); } catch (InterruptedException ex) { //do not respond to interruption too quickly on purpose try { Thread.sleep(1000); } catch (InterruptedException e) {} Thread.currentThread().interrupt(); } System.out.println(Thread.currentThread().getName() + ":" + i); if (Thread.interrupted()) { break; } } } } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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