Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically, what <code>interrupt()</code> does is to set a flag in the <code>Thread</code> object. And you need to check it with <code>isInterrupted()</code>. Then you can handle this interrupt signal. It won't throw an <code>InterruptedException</code> in this situation.</p> <p>Besides, it can cause some methods, for example, <code>Thread.sleep()</code>, <code>Object.wait()</code>, to return immediately and throw an <code>InterruptedException</code>. And you can get and <code>InterruptedException</code> in this situation.</p> <p>From <strong>Java Concurrency in Practice, 7.1.1. Interruption</strong>:</p> <p><code> A good way to think about interruption is that it does not actually interrupt a running thread; it just requests that the thread interrupt itself at the next convenient opportunity. (These opportunities are called cancellation points.) Some methods, such as wait, sleep, and join, take such requests seriously, throwing an exception when they receive an interrupt request or encounter an already set interrupt status upon entry. Well behaved methods may totally ignore such requests so long as they leave the interruption request in place so that calling code can do something with it. Poorly behaved methods swallow the interrupt request, thus denying code further up the call stack the opportunity to act on it. </code> </p> <p>In your above code, you are not waiting/sleeping. So you have to check <code>isInterrupted()</code> and handle interrupt signal yourself in the <code>while</code> loop.</p> <pre><code>while (TestInteruptibility.spin) { if (Thread.currentThread().isInterrupted()) { break; } } </code></pre> <p>References:</p> <ol> <li><a href="https://stackoverflow.com/a/8050257/2086065">why interrupt() not work as expected and how does it work</a></li> <li><a href="https://stackoverflow.com/a/3590008/2086065">What does java.lang.Thread.interrupt() do?</a></li> </ol>
    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.
    3. 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