What is java.lang.InterruptedException in java

What is hierarchy of java.lang.InterruptedException?

-java.lang.Object
-java.lang.Throwable
 -java.lang.Exception
  -java.lang.InterruptedException



java.lang.InterruptedException is Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java ?

java.lang.InterruptedException is a compile time Exception in java.



What is InterruptedException in java?

InterruptedException is thrown when a thread is sleeping, waiting or occupied, and the thread is interrupted, either before or during the activity.


Scenarios where InterruptedException may be thrown in java>


  1. sleep() method throws compile time exception i.e. InterruptedException.

Program to show sleep method throws InterruptedException.
public class InterruptedExceptionSleepExample {
public static void main(String args[]) {
     System.out.println("a");
     try {
          Thread.sleep(1000);//current thread (i.e. main thread) will sleep for 1 second
     } catch (InterruptedException e) {
          e.printStackTrace();
     }
     System.out.println("b");
    
}
}
/*OUTPUT
a
b
*/



  1. wait(long timeout) - Causes the current thread to wait until either another thread invokes the notify() or notifyAll() methods for this object, or a specified timeout time has elapsed.
wait method signature-
public final native void wait(long timeout) throws InterruptedException;

Program to show wait method throws InterruptedException.
class MyRunnable implements Runnable{
public void run(){
     synchronized (this) {
          System.out.println("MyRunnable,in run() method");
          try {
              this.wait(1000); //thread will wait for 1 second
              System.out.println("Thread in runnable state after 1 second");
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
     }
}
}
public class InterruptedExceptionWaitExample  {
public static void main(String[] args) {
     MyRunnable myRunnable=new MyRunnable();
     Thread thread1=new Thread(myRunnable,"Thread-1");
     thread1.start();
}
}
/*OUTPUT
MyRunnable,in run() method
Thread in runnable state after 1 second
*/


  1. wait(long timeout, int nanos) - Causes the current thread to wait until either another thread invokes the notify() or notifyAll() methods for this object, or a specified timeout time plus the specified number of nanoseconds has elapsed.
public static native void wait(long timeout,int nanos) throws InterruptedException;




  1. join() method ensure all threads that started from main must end in order in which they started and also main should end in last.In other words waits for thread to die on which thread has been called.
join() method throws InterruptedException

Program to show join method throws InterruptedException.
class MyRunnable implements Runnable {
public void run() {
     System.out.println("in run() method");
     for (int i = 0; i < 2; i++) {
          System.out.println("i=" + i + " ,ThreadName="
                   + Thread.currentThread().getName());
     }
}
}
public class InterruptedExceptionJoinExample {
public static void main(String... args) {
     System.out.println("In main() method");
     MyRunnable runnable = new MyRunnable();
     Thread thread1 = new Thread(runnable);
     Thread thread2 = new Thread(runnable);
     thread1.start();
     try {
          thread1.join();
     } catch (InterruptedException e) {
          e.printStackTrace();
     }
     thread2.start();
     try {
          thread2.join();
     } catch (InterruptedException e) {
          e.printStackTrace();
     }
     System.out.println("end main() method");
}
}
/*OUTPUT
In main() method
in run() method
i=0 ,ThreadName=Thread-0
i=1 ,ThreadName=Thread-0
in run() method
i=0 ,ThreadName=Thread-1
i=1 ,ThreadName=Thread-1
end main() method
*/


  1. interrupt() method can throw InterruptedException. When thread is interrupted, if it in waiting state caused by invocation of wait(), sleep or join()  method then its interrupt status will be cleared and it will receive an InterruptedException.
For program please refer  interrupt()

Must read for you :

No comments:

Post a Comment