The Thread class in Java, part of the java.lang package, is used to create and manage threads for concurrent execution. It provides the foundation for multithreading in Java by allowing you to define and control threads directly.
Important Concepts
- Definition: The Thread class extends Object and implements the Runnable interface, which means it has a run() method that defines the task to be executed.
- Purpose: It enables the creation and execution of threads, either by extending the Thread class or by passing a Runnable object to a Thread.
Constructor/Method | Description |
---|---|
Constructors | |
Thread() | Creates a new Thread with a default name (e.g., “Thread-n”). |
Thread(Runnable target) | Creates a new Thread with the specified Runnable task and a default name. |
Thread(Runnable target, String name) | Creates a new Thread with the specified Runnable task and custom name. |
Thread(String name) | Creates a new Thread with the specified name. |
Core Execution Methods | |
void start() | Starts the thread, invoking its run() method in a separate thread. |
void run() | Contains the code to be executed by the thread. If a Runnable is provided, it calls the Runnable‘s run() method. Override in subclasses or provide via Runnable. |
Thread State and Information Methods | |
boolean isAlive() | Returns true if the thread is alive (started but not yet terminated). |
Thread.State getState() | Returns the thread’s current state (NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED). |
String getName() | Returns the thread’s name. |
void setName(String name) | Sets the thread’s name. |
Priority and Daemon Status Methods | |
int getPriority() | Returns the thread’s priority (1 to 10, typically MIN_PRIORITY, NORM_PRIORITY, or MAX_PRIORITY). |
void setPriority(int newPriority) | Sets the thread’s priority (1 to 10). Default is NORM_PRIORITY (5). |
boolean isDaemon() | Returns true if the thread is a daemon (background) thread. |
void setDaemon(boolean on) | Marks the thread as a daemon thread (must be called before start()). Daemon threads terminate when all non-daemon threads finish. |
Interruption Handling Methods | |
void interrupt() | Interrupts the thread, setting its interrupt status. Can wake a thread from sleep(), wait(), or join() with an InterruptedException. |
boolean isInterrupted() | Returns true if the thread has been interrupted (without clearing the interrupt status). |
static boolean interrupted() | Returns true if the current thread has been interrupted and clears the interrupt status. |
Thread Synchronization Methods | |
void join() | Causes the calling thread to wait until this thread terminates. |
void join(long millis) | Causes the calling thread to wait up to the specified milliseconds for this thread to terminate. |
Static Utility Methods Methods | |
static Thread currentThread() | Returns a reference to the currently executing thread. |
static void sleep(long millis) | Pauses the current thread for the specified milliseconds. Throws InterruptedException if interrupted. |
static void yield() | Hints to the scheduler that the current thread is willing to yield its CPU time to other threads (platform-dependent). |
The Thread class in Java is a cornerstone of multithreading, enabling developers to create and manage multiple threads of execution within a program. By either extending the Thread
class or implementing the Runnable
interface, Java provides flexible ways to define tasks that can run concurrently. The Thread
class comes equipped with powerful methods like start()
, run()
, sleep()
, join()
, and interrupt()
that allow fine-grained control over thread behavior.