Java Notes
Complete guide to the Thread class in Java — constructors, important methods (sleep, join, yield, interrupt), thread naming, thread groups, and practical usage patterns.
The Thread Class Overview
java.lang.Thread is the class that represents a thread of execution. Every thread in Java is an instance of this class (or its subclass).
Thread Constructors
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Output:
| Default | Thread-0 |
| With Runnable | Thread-1 |
| Named | MyWorker |
| Name only | CustomName |
| With group | GroupThread in MyGroup |
| Running | Thread-1 |
| Running | MyWorker |
Important Thread Methods
Thread.sleep() — Pause Execution
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Thread.join() — Wait for Thread to Finish
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Output:
Thread.yield() — Hint to Scheduler
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Thread.interrupt() — Signal a Thread to Stop
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Output:
Thread Information Methods
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Naming and Thread Groups
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Common Mistakes
1. Calling sleep on a thread object
Thread t = new Thread(() -> { /* work */ });
t.start();
// ❌ WRONG: This sleeps the CURRENT thread, NOT thread t!
t.sleep(1000); // Misleading — compiles but sleeps main thread
// ✅ CORRECT: sleep() is always for the current thread
Thread.sleep(1000); // Clearly shows it affects current thread2. Not handling InterruptedException properly
// ❌ BAD: Swallowing the interrupt
catch (InterruptedException e) { /* ignore */ }
// ✅ GOOD: Either re-interrupt or handle gracefully
catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Restore flag
return; // Stop the thread
}Interview Questions
Q1: What is the difference between sleep() and wait()?
Answer: sleep() is a Thread static method that pauses the current thread for a fixed duration WITHOUT releasing any locks. wait() is an Object method that releases the object's lock and waits until notify()/notifyAll() is called. sleep() is time-based; wait() is notification-based.
Q2: What does join() do?
Answer: join() makes the calling thread wait until the target thread finishes execution. For example, if main calls t.join(), main blocks until thread t completes. It can accept a timeout parameter.
Q3: What is the purpose of yield()?
Answer: yield() is a hint to the thread scheduler that the current thread is willing to give up its CPU time slice to let other threads of equal priority run. It's just a suggestion — the scheduler may ignore it. The thread goes from RUNNING to RUNNABLE state.
Q4: How do you stop a thread in Java?
Answer: Use interrupt() — set the interrupt flag and check isInterrupted() in the thread's run logic, or catch InterruptedException from blocking methods. Never use deprecated stop() method as it can leave objects in inconsistent states.
Q5: Can we call start() twice on the same thread?
Answer: No. The second call throws IllegalThreadStateException. Once a thread has been started (even if it has finished), it cannot be restarted. Create a new Thread instance instead.
Q6: What is a ThreadGroup?
Answer: ThreadGroup organizes threads into groups for collective management. You can interrupt all threads in a group, set maximum priority, list active threads, etc. However, ThreadGroups are rarely used in modern Java — ExecutorService provides better thread management.
Q8: What is the purpose of Thread.yield()?
A: yield() is a hint to the scheduler that the current thread is willing to yield its current time slice. The scheduler may ignore this hint. It's rarely used in practice because it's unreliable and platform-dependent. It does NOT release any locks.
Q9: What is the difference between sleep() and wait()?
A: sleep() pauses the thread for a specified time without releasing locks. wait() releases the monitor lock and waits until another thread calls notify()/notifyAll(). sleep() is a Thread static method; wait() is an Object instance method that must be called inside synchronized blocks.
Q10: How does thread interruption work in Java?
A: thread.interrupt() sets the thread's interrupt flag. If the thread is sleeping/waiting, it throws InterruptedException and clears the flag. If the thread is running, it must check Thread.interrupted() or isInterrupted() and respond. Interruption is cooperative — it's a polite request, not a forced stop.
Q11: Why was Thread.stop() deprecated?
A: stop() abruptly terminates a thread by throwing ThreadDeath, which can leave shared data in an inconsistent state (partially completed operations, locks released without cleanup). Use interruption instead — it's cooperative, allowing the thread to clean up resources and reach a consistent state before stopping.
Q12: What is the difference between isInterrupted() and Thread.interrupted()?
A: isInterrupted() is an instance method that checks the flag without clearing it (for checking other threads). Thread.interrupted() is a static method that checks AND CLEARS the current thread's interrupt flag. Use interrupted() in the thread that's checking its own status; use isInterrupted() to check another thread.
Continue the lesson path