The java.lang.Thread class is the foundation for multithreading in Java. It represents an individual thread of execution and provides methods for managing thread behaviour, controlling execution, and retrieving thread information.
Thread is a concrete class defined in java.lang package
Example: Creating a Thread Using Different Constructors
public class MyTask implements Runnable {
public void run() {
System.out.println("Thread running: " + Thread.currentThread().getName());
}
}
public class ThreadDemo {
public static void main(String[] args) {
Thread t1 = new Thread(); // Default thread
Thread t2 = new Thread(new MyTask()); // Runnable target
Thread t3 = new Thread(new MyTask(), "Worker Thread"); // Runnable with name
t1.start();
t2.start();
t3.start();
}
}
Thread Control Methods
The Thread class provides several methods for controlling execution.
Method
Description
void start()
Starts the thread and calls run() method.
void run()
Defines the task executed by the thread.
static void sleep(long millis)
Makes the current thread sleep for a given time.
void join()
Waits for the thread to finish execution.
void interrupt()
Interrupts a sleeping/waiting thread.
boolean isAlive()
Checks if the thread is still running.
Example
1. start() and run()
start(): Creates a new thread and invokes run() on that thread.
run(): Executes in the current thread (not a new one).
public class MyThread extends Thread {
public void run() {
System.out.println("Running in thread: " + Thread.currentThread().getName());
}
}
public class StartVsRunExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
// Using start() -> Runs in a new thread
t1.start();
// Using run() -> Runs in the main thread
t1.run();
System.out.println("Main thread finished: " + Thread.currentThread().getName());
/*
Running in thread: Thread-0
Running in thread: main
Main thread finished: main
*/
}
}
2. sleep() – Pause Execution
sleep(time) pauses the thread for the given milliseconds.
public class SleepExample extends Thread {
public void run() {
for (int i = 1; i <= 3; i++) {
try {
System.out.println("Sleeping for 1 second...");
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException e) {
System.out.println("Thread interrupted!");
}
System.out.println("Thread running: " + i);
}
}
}
public class SleepDemo {
public static void main(String[] args) {
SleepExample t1 = new SleepExample();
t1.start();
/*
Sleeping for 1 second...
Thread running: 1
Sleeping for 1 second...
Thread running: 2
Sleeping for 1 second...
Thread running: 3
*/
}
}
3. join() – Wait for Another Thread to Finish
join() makes the calling thread wait until the specified thread finishes execution.
public class JoinExample extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread running: " + i);
try {
Thread.sleep(500); // Sleep for 500ms
} catch (InterruptedException e) {
System.out.println("Thread interrupted!");
}
}
}
}
public class JoinDemo {
public static void main(String[] args) {
JoinExample t1 = new JoinExample();
JoinExample t2 = new JoinExample();
t1.start();
try {
t1.join(); // Main thread waits for t1 to finish
} catch (InterruptedException e) {
System.out.println("Main thread interrupted!");
}
t2.start(); // t2 starts only after t1 is done
/*
Thread running: 1
Thread running: 2
Thread running: 3
Thread running: 4
Thread running: 5
Thread running: 1
Thread running: 2
Thread running: 3
Thread running: 4
Thread running: 5
*/
}
}
4. interrupt() – Stop a Sleeping/Waiting Thread
interrupt() signals a thread to stop execution if it is sleeping or waiting.
public class InterruptExample extends Thread {
public void run() {
try {
System.out.println("Thread going to sleep...");
Thread.sleep(5000); // Sleep for 5 seconds
System.out.println("Thread woke up!");
} catch (InterruptedException e) {
System.out.println("Thread interrupted while sleeping!");
}
}
}
public class InterruptDemo {
public static void main(String[] args) {
InterruptExample t1 = new InterruptExample();
t1.start();
try {
Thread.sleep(2000); // Let thread run for 2 seconds
} catch (InterruptedException e) {
System.out.println("Main thread interrupted!");
}
t1.interrupt(); // Interrupt t1 while sleeping
/*
Thread going to sleep...
Thread interrupted while sleeping!
*/
}
}
5. isAlive() – Check if Thread is Running
isAlive() returns true if the thread is still running, else false.
public class AliveExample extends Thread {
public void run() {
try {
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException e) {
System.out.println("Thread interrupted!");
}
System.out.println("Thread finished execution.");
}
}
public class IsAliveDemo {
public static void main(String[] args) {
AliveExample t1 = new AliveExample();
System.out.println("Before start: " + t1.isAlive()); // false
t1.start();
System.out.println("After start: " + t1.isAlive()); // true
try {
t1.join(); // Wait for t1 to finish
} catch (InterruptedException e) {
System.out.println("Main thread interrupted!");
}
System.out.println("After join: " + t1.isAlive()); // false
/*
Before start: false
After start: true
Thread finished execution.
After join: false
*/
}
}
Thread Property Methods
The Thread class provides several methods for thread properties.
Method
Description
setvoid setName(String name)
Sets the thread’s name.
String getName()
Returns the thread’s name.
void setPriority(int priority)
Sets thread priority (1-10).
int getPriority()
Gets thread priority.
static Thread currentThread()
Returns the currently running thread.
void setDaemon(boolean status)
Marks a thread as daemon.
boolean isDaemon()
Checks if the thread is daemon.
public class MyThread extends Thread {
public void run() {
System.out.println("Thread Name: " + getName());
System.out.println("Thread Priority: " + getPriority());
System.out.println("Is Daemon: " + isDaemon());
}
}
public class ThreadPropertiesExample {
public static void main(String[] args) {
// Creating threads
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread daemonThread = new MyThread();
// Setting names
t1.setName("Worker-1");
t2.setName("Worker-2");
daemonThread.setName("BackgroundDaemon");
// Setting priorities
t1.setPriority(Thread.MIN_PRIORITY); // Priority = 1
t2.setPriority(Thread.MAX_PRIORITY); // Priority = 10
// Setting daemon thread
daemonThread.setDaemon(true); // Making daemon thread
// Starting threads
t1.start();
t2.start();
daemonThread.start();
/*
Thread Name: Worker-1
Thread Priority: 1
Is Daemon: false
Thread Name: Worker-2
Thread Priority: 10
Is Daemon: false
Thread Name: BackgroundDaemon
Thread Priority: 5
Is Daemon: true
*/
}
}
Thread State Methods
The Thread class provides several methods for state management.
Method
Description
Thread.State getState()
Returns the thread’s state.
void yield()
Suggests the scheduler to allow other threads to execute.
public class MyThread extends Thread {
public void run() {
System.out.println(getName() + " - State: " + getState()); // Running State
for (int i = 1; i <= 3; i++) {
System.out.println(getName() + " is running: " + i);
Thread.yield(); // Giving chance to other threads
}
System.out.println(getName() + " - State After Completion: " + getState());
}
}
public class ThreadStateYieldExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
System.out.println(t1.getName() + " - Initial State: " + t1.getState()); // NEW
t1.start();
t2.start();
System.out.println(t1.getName() + " - State After Start: " + t1.getState()); // RUNNABLE
System.out.println(t2.getName() + " - State After Start: " + t2.getState());
try {
t1.join(); // Waiting for t1 to finish
t2.join(); // Waiting for t2 to finish
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(t1.getName() + " - Final State: " + t1.getState()); // TERMINATED
System.out.println(t2.getName() + " - Final State: " + t2.getState()); // TERMINATED
/*
Thread-0 - Initial State: NEW
Thread-0 - State After Start: RUNNABLE
Thread-1 - State After Start: RUNNABLE
Thread-0 - State: RUNNABLE
Thread-0 is running: 1
Thread-1 - State: RUNNABLE
Thread-1 is running: 1
Thread-0 is running: 2
Thread-1 is running: 2
Thread-0 is running: 3
Thread-1 is running: 3
Thread-0 - State After Completion: RUNNABLE
Thread-1 - State After Completion: RUNNABLE
Thread-0 - Final State: TERMINATED
Thread-1 - Final State: TERMINATED
*/
}
}
Thread Interruption Methods
Method
Description
void interrupt()
Interrupts a thread.
boolean isInterrupted()
Checks if a thread is interrupted.
static boolean interrupted()
Checks and clears the interrupt status of the current thread.
class MyThread extends Thread {
public void run() {
System.out.println(getName() + " started.");
// Checking interrupted status in a loop
for (int i = 1; i <= 5; i++) {
if (isInterrupted()) { // Checks if thread is interrupted
System.out.println(getName() + " was interrupted. Exiting...");
return; // Exit if interrupted
}
System.out.println(getName() + " is working: " + i);
try {
Thread.sleep(1000); // Simulating work
} catch (InterruptedException e) {
System.out.println(getName() + " caught InterruptedException.");
return; // Exit after catching InterruptedException
}
}
System.out.println(getName() + " completed.");
}
}
public class InterruptExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
try {
Thread.sleep(2500); // Let the thread run for a while
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main thread is interrupting " + t1.getName());
t1.interrupt(); // Interrupt the thread
/*
Thread-0 started.
Thread-0 is working: 1
Thread-0 is working: 2
Main thread is interrupting Thread-0
Thread-0 caught InterruptedException.
*/
}
}
Thread Synchronization
When multiple threads access shared resources, synchronization is needed.
Technique
Description
synchronized
Prevents multiple threads from accessing critical sections.
lock
Uses Lock interface (ReentrantLock).
volatile
Ensures visibility of changes to variables across threads.
atomic
Uses classes like AtomicInteger to prevent race conditions.
Example: Synchronized Block
class SharedResource {
synchronized void printNumbers() {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}
public class SyncDemo {
public static void main(String[] args) {
SharedResource obj = new SharedResource();
new Thread(obj::printNumbers).start();
new Thread(obj::printNumbers).start();
}
}