GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||
GeezerVison is showing www.tutorialspoint.com — Java - InterruptedException
Selected Reading
Java - InterruptedExceptionThe 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:
Constructors of InterruptedExceptionThere are two constructors of InterruptedException class:
Methods of InterruptedExceptionThere are some methods of InterruptedException class:
Example of InterruptedExceptionIn 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();
}
}
OutputFollowing 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 InterruptedExceptionIn 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();
}
}
OutputFollowing 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:
Avoiding InterruptedException using Thread.interrupted() methodIn 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
}
}
OutputFollowing is the output of the above code: Thread running... Thread running... Thread running... Thread interrupted! Exiting... java_lang_exceptions.htm Advertisements
| ||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |