Creating Threads
In Java, there are two primary ways to create threads:
- Extending the
Threadclass. - Implementing the
Runnableinterface.
Both approaches allow you to define the task a thread should execute using the run() method.
1. Extending the Thread Class​
To create a thread by extending the Thread class, override its run() method. The start() method is used to begin execution.
Example: Extending the Thread Class​
class MyThread extends Thread {
public void run() {
System.out.println("Thread running: " + Thread.currentThread().getName());
}
}
public class ThreadExample {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.setName("MyThread-1");
thread.start(); // Start the thread
}
}
Output:
Thread running: MyThread-1
Explanation:
- The
MyThreadclass extendsThreadand overrides therun()method. - The
start()method invokes therun()method in a new thread.
2. Implementing the Runnable Interface​
The Runnable interface provides a more flexible way to create threads. It allows a class to define the run() method without inheriting from Thread.
Example: Implementing the Runnable Interface​
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread running: " + Thread.currentThread().getName());
}
}
public class RunnableExample {
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable(), "MyRunnable-1");
thread.start(); // Start the thread
}
}
Output:
Thread running: MyRunnable-1
Explanation:
- The
MyRunnableclass implements theRunnableinterface and defines therun()method. - A
Threadobject is created with theRunnableinstance, and thestart()method begins execution.
Key Differences Between the Two Approaches​
| Aspect | Extending Thread | Implementing Runnable |
|---|---|---|
| Inheritance | Limits flexibility (Java does not support multiple inheritance). | More flexible (can extend another class). |
| Use Case | Suitable for simple thread creation. | Preferred for complex applications. |
Key Takeaways​
- Use the
Threadclass for quick and simple thread creation. - Use the
Runnableinterface for better flexibility and reusability. - Always call the
start()method to begin thread execution (not therun()method directly).