Interrupt a sleeping Thread
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Java, you do not forcibly wake a sleeping thread by killing it. The normal mechanism is interruption: one thread calls interrupt(), and the sleeping thread receives an InterruptedException and decides how to shut down or continue.
What interrupt() Actually Does
Each Java thread has an interrupt status. Calling thread.interrupt() sets that status. If the target thread is blocked in Thread.sleep(), Object.wait(), or Thread.join(), Java clears the status and throws InterruptedException instead.
That detail matters because interruption is cooperative. It is a request to stop what the thread is doing, not a guarantee that execution ends immediately.
Here is a minimal example with a sleeping worker:
After about one second, the main thread interrupts the worker. The worker leaves sleep() immediately and enters the catch block.
Handle InterruptedException Correctly
The right response depends on the role of the thread.
If the thread should stop, the cleanest solution is usually to log if needed and return:
Notice the call to Thread.currentThread().interrupt(). When InterruptedException is thrown, the interrupted flag has already been cleared. Restoring it preserves the signal for higher-level code or for diagnostics.
If your method can declare throws InterruptedException, that is often even better. It lets the caller decide the shutdown policy instead of swallowing the signal.
Interrupting Threads That Are Not Sleeping
A compute-heavy loop will not throw InterruptedException on its own. It must check the interrupted status explicitly.
This pattern is essential for CPU-bound work. If you never check isInterrupted(), the interrupt request is effectively ignored.
Executor Services and Cancellation
In real applications, threads are often managed through an ExecutorService rather than by creating Thread objects directly. In that model, interruption still matters because Future.cancel(true) sends an interrupt to the running task.
That is why interruption-aware code is worth writing even when you are not manipulating raw threads.
Common Pitfalls
A common mistake is catching InterruptedException and doing nothing. That discards the cancellation request and makes shutdown unreliable.
Another error is assuming interrupt() forcefully stops any code path. It only has immediate effect when the thread is in an interruptible blocking call or when your code checks the flag.
Some developers also reach for deprecated mechanisms such as Thread.stop(). Do not use them. They can leave shared state inconsistent and are not a safe replacement for interruption.
Finally, avoid wrapping long loops in a broad catch (Exception e) block that hides InterruptedException. If interruption is part of your control flow, treat it explicitly.
Summary
- Use
thread.interrupt()to request that another thread stop waiting or stop soon - '
Thread.sleep()reacts by throwingInterruptedException' - Restore the interrupt status if you catch the exception and then exit or rethrow
- Busy loops must check
isInterrupted()themselves - '
Future.cancel(true)relies on the same interruption mechanism'
Related reading
- Interview Questions on Socket Programming and Multi-Threading
- Invalid token 'void' when compling async..await on build server
- invoke aws lambda from another lambda asynchronously
- Invoke or BeginInvoke cannot be called on a control until the window handle has been created
- Intersection and union of ArrayLists in Java
- ''Invalid bean definition'' when migrating Spring Boot 2.0.6 to 2.1.0 with EvaluationContextExtensionSupport and custom PermissionEvaluator
- IO Completion Ports IOCP
- iOS - Ensure execution on main thread

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.