Introduction
The Thread class in Java allows the creation and management of threads, enabling concurrent execution in applications.
Table of Contents
- What is the
ThreadClass? - Common Methods
- Examples of Using the
ThreadClass - Conclusion
1. What is the Thread Class?
The Thread class represents a thread of execution in a Java program. It provides methods to manage thread lifecycle, priority, and behavior.
2. Common Methods
start(): Begins the execution of a thread.run(): Contains the code that defines the thread’s task.sleep(long millis): Causes the thread to sleep for a specified time.join(): Waits for the thread to finish execution.interrupt(): Interrupts the thread.isAlive(): Checks if the thread is still running.getId(): Returns the thread’s unique ID.getName(): Gets the name of the thread.setName(String name): Sets the name of the thread.getPriority(): Returns the thread’s priority.setPriority(int newPriority): Sets the thread’s priority.getState(): Returns the current state of the thread.yield(): Causes the currently executing thread to pause and allow other threads to execute.
3. Examples of Using the Thread Class
Example 1: Using start() and run()
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running.");
}
}
public class StartRunExample {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // Starts the thread
}
}
Output:
Thread is running.
Example 2: Using sleep()
public class SleepExample {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
try {
System.out.println("Thread is sleeping for 2 seconds.");
Thread.sleep(2000);
System.out.println("Thread awake.");
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
}
});
thread.start();
}
}
Output:
Thread is sleeping for 2 seconds.
Thread awake.
Example 3: Using join()
public class JoinExample {
public static void main(String[] args) {
Thread thread = new Thread(() -> System.out.println("Thread finished execution."));
thread.start();
try {
thread.join(); // Waits for the thread to finish
System.out.println("Main thread resumed.");
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
}
}
}
Output:
Thread finished execution.
Main thread resumed.
Example 4: Using interrupt()
public class InterruptExample {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("Thread was interrupted.");
}
});
thread.start();
thread.interrupt(); // Interrupts the thread
}
}
Output:
Thread was interrupted.
Example 5: Using isAlive()
public class IsAliveExample {
public static void main(String[] args) {
Thread thread = new Thread(() -> System.out.println("Thread is running."));
thread.start();
System.out.println("Is thread alive? " + thread.isAlive());
}
}
Output:
Thread is running.
Is thread alive? true
Example 6: Using getId()
public class GetIdExample {
public static void main(String[] args) {
Thread thread = new Thread(() -> System.out.println("Thread ID: " + Thread.currentThread().getId()));
thread.start();
}
}
Output:
Thread ID: 20
Example 7: Using getName() and setName()
public class GetSetNameExample {
public static void main(String[] args) {
Thread thread = new Thread(() -> System.out.println("Thread name: " + Thread.currentThread().getName()));
thread.setName("CustomThread");
thread.start();
}
}
Output:
Thread name: CustomThread
Example 8: Using getPriority() and setPriority()
public class GetSetPriorityExample {
public static void main(String[] args) {
Thread thread = new Thread(() -> System.out.println("Thread priority: " + Thread.currentThread().getPriority()));
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
}
}
Output:
Thread priority: 10
Example 9: Using getState()
public class GetStateExample {
public static void main(String[] args) {
Thread thread = new Thread(() -> {});
System.out.println("Thread state: " + thread.getState());
thread.start();
System.out.println("Thread state after start: " + thread.getState());
}
}
Output:
Thread state: NEW
Thread state after start: RUNNABLE
Example 10: Using yield()
public class YieldExample {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
System.out.println("Thread yielding.");
Thread.yield();
System.out.println("Thread resumed.");
});
thread.start();
}
}
Output:
Thread yielding.
Thread resumed.
4. Conclusion
The Thread class in Java provides a comprehensive set of methods for managing thread lifecycle, priority, and behavior. Understanding and using these methods effectively can enhance concurrent programming in Java applications.