Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - InterruptedException



The Java InterruptedException is thrown when a thread is waiting, sleeping, or doing some processing, and it is interrupted. The InterruptedException is a checked exception that is thrown when a thread is waiting, sleeping, or doing some processing, and it is interrupted.

Following is the reason when JVM throws an InterruptedException in Java:

  • When a thread is doing some processing, and it is interrupted, JVM throws an InterruptedException.

Constructors of InterruptedException

There are two constructors of InterruptedException class:

  • InterruptedException(): This constructor is used to create an InterruptedException object without any message.
  • InterruptedException(String message): This constructor is used to create an InterruptedException object with a message.

Methods of InterruptedException

There are some methods of InterruptedException class:

Method Description
getMessage() It is used to return the message of the exception.
toString() It is used to return the detail message string of the exception.
printStackTrace() It is used to print the stack trace of the exception.

Example of InterruptedException

In this example, we are creating a thread that is sleeping for 5 seconds. We are interrupting the thread while it is sleeping, so JVM will throw an InterruptedException.

public class InterruptedExceptionExample extends Thread {
   public void run() {
         Thread.sleep(5000);
   }
   public static void main(String[] args) {
      InterruptedExceptionExample t1 = new InterruptedExceptionExample();
      t1.start();
      t1.interrupt();
   }
}

Output

Following is the output of the above code:

InterruptedExceptionExample.java:3: error: unreported exception InterruptedException; must be caught or declared to be thrown
         Thread.sleep(5000);
                     ^
1 error

As you can see in the output, JVM throws an InterruptedException because we are interrupting the thread while it is sleeping.

Handling InterruptedException

In this example, we are creating a thread that is sleeping for 5 seconds. We are interrupting the thread while it is sleeping, so JVM will throw an InterruptedException. We are handling this exception using try-catch block.

public class InterruptedExceptionExample extends Thread {
   public void run() {
      try {
         Thread.sleep(5000);
      } catch (InterruptedException e) {
         System.out.println("Thread is interrupted");
      }
   }
   public static void main(String[] args) {
      InterruptedExceptionExample t1 = new InterruptedExceptionExample();
      t1.start();
      t1.interrupt();
   }
}

Output

Following is the output of the above code:

Thread is interrupted

As you can see in the output, we are handling the InterruptedException using the try-catch block.

How avoid InterruptedException in Java?

There are some ways to avoid the InterruptedException in Java:

  • Use the Thread.interrupted() method to check if the current thread is interrupted or not.
  • Use the Thread.currentThread().isInterrupted() method to check if the current thread is interrupted or not.
  • Use the Thread.sleep() method with the TimeUnit class to avoid the InterruptedException.

Avoiding InterruptedException using Thread.interrupted() method

In this example, we are creating a thread that is sleeping for 5 seconds. We are interrupting the thread while it is sleeping. We are using the Thread.interrupted() method to check if the current thread is interrupted or not.

public class HandleInterruptedException extends Thread {
   public void run() {
      while (!Thread.interrupted()) {  // Check if thread is interrupted
         try {
            System.out.println("Thread running...");
            Thread.sleep(1000);
         } catch (InterruptedException e) {
            System.out.println("Thread interrupted! Exiting...");
            break;  // Exit the loop when interrupted
         }
      }
   }
   public static void main(String[] args) throws InterruptedException {
      HandleInterruptedException t1 = new HandleInterruptedException();
      t1.start();

      Thread.sleep(3000); // Let it run for 3 seconds
      t1.interrupt();     // Interrupt the thread
   }
}

Output

Following is the output of the above code:

Thread running...
Thread running...
Thread running...
Thread interrupted! Exiting...
java_lang_exceptions.htm
Advertisements