When does Java's Thread.sleep throw InterruptedException?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Java's Thread.sleep() is a fundamental method for suspending the execution of the current thread for a specified number of milliseconds. However, when dealing with thread interruptions, developers often encounter the InterruptedException. This article delves into the circumstances under which Thread.sleep throws InterruptedException, augmented by technical explanations and examples.
Understanding the Basics of Thread.sleep
Thread.sleep() is an instance method used to pause the execution of a thread for a specified period, expressed in milliseconds. It doesn't consume resources while in this state, making it efficient for purposes such as pausing repeated tasks or providing deliberate delays. Here's a basic usage example:
When Does InterruptedException Occur?
The Nature of InterruptedException
InterruptedException is a checked exception that signals that a thread waiting, sleeping, or otherwise occupied with some form of processing was interrupted. In the case of Thread.sleep(), the exception is thrown under certain circumstances:
- Thread State: The current thread was interrupted while it was in a sleeping state.
- Interruption Request: An interrupt signal was sent to the sleeping thread via the
Thread.interrupt()method.
Technical Description
The Java Language Specification describes that if any thread has interrupted the current thread, the Thread.sleep() method will throw InterruptedException. This is crucial because it provides a mechanism for cooperative thread termination, allowing other parts of the program to handle the interruption gracefully.
Practical Example
Let's consider an example where we intentionally interrupt a thread while it is in a sleeping state:
Output
In this example:
- The
sleepingThreadstarts execution and attempts to sleep for 5 seconds. - The main thread sleeps for 1 second before interrupting
sleepingThread. - Once interrupted,
sleepingThreadcatches theInterruptedException.
Handling InterruptedException
When catching InterruptedException, it is essential to decide how your application should respond. Generally, developers take one of the following approaches:
- Log and Ignore: Use this approach if the interruption is trivial.
- Re-interrupt: Important when the current method doesn't handle the interruption. It is good practice to preserve the interruption status of a thread:
- Cleanup and Abort: Perform necessary cleanup tasks and gracefully terminate the process.
Summary Table
| Description | |
| Method | Thread.sleep(long millis) |
| Throws | InterruptedException |
| When | If the thread is interrupted during sleep |
| Common Handling | Log & Ignore, Re-interrupt, Cleanup & Abort |
| Interrupt Method | Thread.interrupt() |
Additional Topics
- Thread Interruption and Concurrency: Understanding interruption is crucial for building responsive applications. It also plays a vital role in concurrent programming where cooperative thread termination is necessary for robustness.
- Alternatives to Thread.sleep: Consider
ScheduledExecutorServicefor more complex scheduling tasks to avoid certain pitfalls associated with arbitrary sleep.
In summary, interruption handling in Java, especially concerning Thread.sleep(), is essential for developing robust multithreaded applications. Proper understanding and handling of InterruptedException ensure that your application can operate seamlessly, even with unexpected interruptions.
Related reading
- When does setTimeout start counting down?
- When exactly are synchronous continuations dangerous?
- When exactly do you use the volatile keyword in Java?
- When is a condition variable needed, isn''t a mutex enough?
- When exactly is it leak safe to use (anonymous) inner classes?
- When exactly is it leak safe to use anonymous inner classes?
- When I catch an exception, how do I get the type, file, and line number?
- When I run sudo minikube start --vm-drivernone it gives me error

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.