How to Create a Thread in Java?

In Java, creating threads is an essential aspect of programming. Threads are used to execute multiple tasks concurrently, improving the performance and responsiveness of programs. In this article, we will discuss how to create threads in Java.

There are two ways to create threads in Java: extending the Thread class and implementing the Runnable interface.

Extending the Thread class:

To create a new thread by extending the Thread class, you need to create a subclass of Thread and override the run() method. The run() method is the entry point for the new thread and is where you put the code that you want the thread to execute.

Here is an example of creating a thread by extending the Thread class:

public class MyThread extends Thread {
public void run() {
System.out.println("MyThread is running");
}
}
public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
}
}


In this example, we create a new thread called MyThread by extending the Thread class. We override the run() method to print a message when the thread is running. In the main() method, we create a new instance of the MyThread class and call the start() method to start the thread.

Implementing the Runnable interface:

To create a new thread by implementing the Runnable interface, you need to create a class that implements the Runnable interface and override the run() method. The run() method is the entry point for the new thread and is where you put the code that you want the thread to execute.

Here is an example of creating a thread by implementing the Runnable interface:

public class MyRunnable implements Runnable {
public void run() {
System.out.println("MyRunnable is running");
}
}

public class Main {
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
}
}


In this example, we create a new class called MyRunnable that implements the Runnable interface. We override the run() method to print a message when the thread is running. In the main() method, we create a new instance of the MyRunnable class and pass it as an argument to the Thread constructor. We then call the start() method to start the thread.

Differences between extending the Thread class and implementing the Runnable interface:

While both ways of creating threads in Java work, there are some differences between the two approaches. Extending the Thread class allows you to override methods like getName() and interrupt() and use the Thread class as a reference. Implementing the Runnable interface allows for more flexibility, as you can extend other classes as well.

Another difference is that implementing the Runnable interface separates the thread’s behavior from the thread’s identity, which can be useful in some cases. For example, if you have a class that needs to implement multiple interfaces, you can use the Runnable interface to create a thread without needing to extend the Thread class.

Conclusion:

Creating threads is a crucial aspect of programming in Java. Whether you choose to extend the Thread class or implement the Runnable interface, understanding how to create threads is essential for creating efficient and responsive programs. By properly using threads, you can improve the performance and responsiveness of your programs, making them more user-friendly and effective.