Question

public class TwoThreads {
        private static Object resource = new Object();

        private static void delay(long n) {
            try 
            { 
                Thread.sleep(n);
            }
            catch (Exception e)
            { 

                e.printStackTrace();
            }
        }

        public static void main(String[] args) {
            System.out.print("StartMain ");
            new Thread1().start();
            delay(1000);                       //dealay 1
            Thread t2 = new Thread2();
            t2.start();   
            delay(1000);                      // delay 2    
            t2.interrupt();                   //step 7
            delay(1000);                      //delay 3
            System.out.print("EndMain ");
        }

        static class Thread1 extends Thread {
            public void run() {
                synchronized (resource) {
                    System.out.print("Startl ");
                    delay(6000);
                    System.out.print("End1 ");
                }
            }
        }

        static class Thread2 extends Thread {
            public void run() {
                synchronized (resource) {
                    System.out.print("Start2 ");
                    delay(2000);
                    System.out.print("End2 ");
                }
            }
        }
    }

At step 7 (as I have marked), main thread calls interrupt() on thread t2, but as it was waiting to acquire the lock on a resource, it doesn't throw any exception. After that, main thread prints "End Main" after waiting 1000 ns. In other words, main thread has completed its task, so what triggers t2.interrupt() again because it throws exception after that?

Was it helpful?

Solution

Here is how your program runs, with timestamps:

0000 StartMain 
0000 Startl 
3000 EndMain 
6000 End1 
6000 Start2 
6000 End2 

Why (timestamps in brackets)?

  • [0000] main launches Thread1, which acquires a lock and sleeps for 6 seconds
  • [1000] main launches Thread2, which can't acquire the lock held by Thread1 for 6 seconds
  • [2000] main interrupts Thread2 setting its interrupted flag to true, but Thread2 is waiting for a lock and does not do anything about it
  • [3000] main ends
  • [6000] Thread1 finishes sleeping and releases the lock
  • [6000] Thread2 can acquire it and starts to sleep (its interrupted flag is still on)
  • [6000] sleep detects that Thread2 has been interrupted and throws an exception immediately
  • [6000] Thread2 finishes, allowing the JVM to exit

OTHER TIPS

You need a ReentrantLock.

public class TwoThreads {
  private static Lock lock = new ReentrantLock();

  private static void delay(long n) {
    try {
      Thread.sleep(n);
    } catch (Exception e) {

      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    System.out.print("StartMain ");
    new Thread1().start();
    delay(1000);                       //dealay 1
    Thread t2 = new Thread2();
    t2.start();
    delay(1000);                      // delay 2    
    t2.interrupt();                   //step 7
    delay(1000);                      //delay 3
    System.out.print("EndMain ");
  }

  static class Thread1 extends Thread {
    public void run() {
      try {
        lock.lockInterruptibly();
        try {
          System.out.print("Startl ");
          delay(6000);
          System.out.print("End1 ");
        } finally {
          lock.unlock();
        }
      } catch (InterruptedException ex) {
        // Interrupted.
      }
    }

  }

  static class Thread2 extends Thread {
    public void run() {
      try {
        lock.lockInterruptibly();
        try {
          System.out.print("Start2 ");
          delay(2000);
          System.out.print("End2 ");
        } finally {
          lock.unlock();
        }
      } catch (InterruptedException ex) {
        // Interrupted.
      }
    }

  }
}

Prints:

StartMain Startl EndMain End1

It´s because the JVM kills your threads when the main thread is shutting down.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top