In Java, a thread is a separate execution path within a program. A thread allows a program to perform multiple tasks concurrently, thereby increasing its efficiency and responsiveness. Each thread has its own call stack and can run concurrently with other threads, sharing the same resources of the program.
Java threads are implemented using the Thread class, which is part of java.lang package. A thread can be created by extending the Thread class and overriding its run method, or by implementing the Runnable interface and passing an instance of the implementing class to a Thread object’s constructor.
Here’s an example of creating a thread by extending the Thread class:
public class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
In this example, we create a MyThread class that extends the Thread class and overrides its run method. We then create an instance of the MyThread class and call its start method, which causes the run method to be executed in a new thread.
Here’s an example of creating a thread by implementing the Runnable interface:
public class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread running");
}
public static void main(String[] args) {
MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();
}
}
In this example, we create a MyRunnable class that implements the Runnable interface and overrides its run method. We then create an instance of the MyRunnable class and pass it to the Thread constructor. We call the start method on the Thread object to start the new thread.
Java provides several methods for controlling the behavior of threads, such as sleep, yield, and join. The sleep method causes the current thread to pause for a specified amount of time, while the yield method suggests to the JVM that the current thread is willing to yield its current use of the CPU. The join method waits for a thread to terminate before continuing execution.
Here’s an example of using the sleep method:
public class SleepExample {
public static void main(String[] args) {
System.out.println("Start");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End");
}
}
In this example, we use the sleep method to pause the main thread for 5 seconds before printing “End”.
Here’s an example of using the join method to wait for a thread to complete:
public class MyThread extends Thread {
public void run() {
// Code to be executed in the thread
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // Start the thread
try {
thread.join(); // Wait for the thread to complete
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread completed.");
}
}
In this example, we create a new thread and start its execution. We then call the join method on the thread to wait for it to complete. Once the thread has been completed, we print a message to the console.
Java threads can be a powerful tool for improving the performance and responsiveness of your applications. However, it’s important to be careful when using threads, as they can also introduce concurrency issues and other problems if not used correctly. It’s therefore important to understand the principles of concurrency and synchronization and to test your code thoroughly to ensure that it works correctly under all conditions.