Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy was task1 thread not interrupted
    primarykey
    data
    text
    <p>Assume the below code is executed with a debugger so that we can predict the order of execution.</p> <ul> <li>t1 -- Here task1 starts working on some long task.</li> <li>t2 --- task2 gets blocked @ Syncronized statement because task1 is holding lock.</li> <li>t3 -- task2 is interrupted but its missed because task2 is using intrinsic locks and hence cannot be interrupted @ synchronized. (Renenterant.lockInterruptible() would have thrown InterruptedExecption).</li> <li>t4 --- task1 is interrupted. However despite of doing the complex task in try catch block, InterruptedExecption was never thrown. Why is that ?</li> </ul> <p>Code:</p> <pre><code>public class TestInteruptibility { public static Object lock = new Object(); public static boolean spin = true; public static void main(String[] args) { Thread task1 = new Thread(new Task(), "Task1"); Thread task2 = new Thread(new Task(), "Task2"); Thread notifier1 = new Thread(new Notifier(), "Notifier1"); task1.start(); task2.start(); task2.interrupt(); task1.interrupt(); notifier1.start(); } } class Task implements Runnable { public void run() { synchronized (TestInteruptibility.lock) { System.out.println("Performing Long Task"); try { while (TestInteruptibility.spin) { } System.out.println("Finsihed Performing Long Task"); TestInteruptibility.lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); System.out.println("I got interrupted while i was waiting @ wait()"); } System.out.println("Ending Task"); } } } class Notifier implements Runnable { public void run() { synchronized (TestInteruptibility.lock) { System.out.println("Performing notification"); TestInteruptibility.lock.notify(); System.out.println("Ending notification"); } } } </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.
 

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