Home » Java Basics » exceptions » java.lang.InterruptedException – How to handle InterruptedException

About Sotirios-Efstathios Maneas

Sotirios-Efstathios Maneas
Sotirios-Efstathios (Stathis) Maneas is a postgraduate student at the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. His main interests include distributed systems, web crawling, model checking, operating systems, programming languages and web applications.

java.lang.InterruptedException – How to handle InterruptedException

In this example we will discuss about InterruptedException in Java. This exception is thrown when a thread is interrupted. The thread could be in either waiting, sleeping or running state and this exception can be thrown either before or during a thread’s activity.

This exception extends the Exception class and thus, can be classified as a checked exception. Checked exceptions must be explicitly declared in a method’s or constructor’s throw clause.

Finally, the InterruptedException exists since the 1.0 version of Java.


The Structure of InterruptedException


  • InterruptedException()
  • Creates an instance of the InterruptedException class, setting null as its message.

  • InterruptedException(String s)
  • Creates an instance of the InterruptedException class, using the specified string as message. The string argument indicates the name of the class that threw the error.

The InterruptedException in Java

A sample example where a thread is being interrupted is shown below:


import java.util.concurrent.TimeUnit;

public class SampleThread extends Thread {
	public SampleThread() {
		System.out.println("An instance of the " + SampleThread.class + " class was created!");
	public void run() {
		try {
			/* Sleep for some seconds. */
		catch(InterruptedException ex) {
			System.err.println("An InterruptedException was caught: " + ex.getMessage());

First, we define the SampleThread class, which extends the Thread class and overrides its run method.


public class InterruptedExceptionExample {
	public static void main(String[] args) throws InterruptedException {
		// Create a new thread.
		Thread thread = new SampleThread();
		//Start the thread's execution.
		//Interrupt the thread.
		//Join the thread.

Then, we create an instance of the SampleThread and interrupt it during its execution. A sample execution is shown below:

An instance of the class main.java.SampleThread class was created!
An InterruptedException was caught: sleep interrupted

More about the InterruptedException in Java

As we already mentioned, every thread has a boolean flag that represents its interrupted status. Initially, this status is set to false for every thread. If a thread is interrupted during an interruptible method such as join or sleep, it will unblock and throw an InterruptedException. In any other case, the thread’s interruption will only update the thread’s interrupted status. A thread can test if its interrupted using one of the following methods:

Notice that the 2nd method does not affect the interrupted status of the thread, in contrast to the 1st method which clears the interrupted status of the calling thread. A second call to the Thread::interrupted() method returns false, unless the calling thread was interrupted again.

How to deal with the InterruptedException

  • The most simple strategy is to throw the InterruptedException to the upper layer of the calling stack and let the upper layer handle it.
  • Another strategy is to catch the InterruptedException, perform some memory cleanup and then, propagate the exception to the upper layer of the calling stack.
  • Moreover, for those cases where a method cannot throw an InterruptedException, you can use the following method, in order to interrupt the current thread:
  •         Thread.currentThread().interrupt();

    This approach can be very useful, because once a Thread catches an InterruptedException, its interrupted status is cleared and instead of ignoring the exception, we restore the interrupted status of the current Thread.

  • Finally, never ignore or swallow the InterruptedException.

Download the Eclipse Project

The Eclipse project of this example: InterruptedExceptionExample.zip.

This was a tutorial about InterruptedException in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.