Understanding the Need for Multithreading: Creating and Starting Threads in Java
Welcome back to our series on Java Multithreading from Beginner to Expert. In our first article, we explored the importance of multithreading, its benefits, and some basic terminology. Now, it’s time to get our hands dirty and dive into the basics of creating and starting threads in Java.
Creating Threads in Java
In Java, there are two primary ways to create a thread:
- Extending the
Thread
class - Implementing the
Runnable
interface
Let’s take a closer look at each approach. 1. Extending the Thread
Class When you extend the Thread
class, you need to override the run
method where you define the code that should execute in the new thread.
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // Start the thread
}
}
Here, we create a class MyThread
that extends Thread
and overrides the run
method. The start
method is called to begin execution of the new thread. 2. Implementing the Runnable
Interface Implementing the Runnable
interface is generally preferred because it allows the class to extend another class if needed. Here’s how you can do it:
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread is running...");
}
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start(); // Start the thread
}
}
In this example, we implement the Runnable
interface and pass an instance of MyRunnable
to a new Thread
object. The start
method is then called to begin the thread’s execution.
Starting Threads
Once a thread is created, it doesn’t start executing right away. You need to call the start
method, which internally calls the run
method. Key points to remember:
start
Method: This method is used to start the execution of the thread. It schedules the thread to run and immediately returns.run
Method: This method contains the code that should run in the new thread. It is called by thestart
method internally.
Example: Creating Multiple Threads
Let’s look at a more practical example where we create multiple threads to perform a simple task.
class Task implements Runnable {
private String threadName;
Task(String name) {
threadName = name;
}
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(threadName + " - Count: " + i);
try {
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
}
System.out.println(threadName + " finished.");
}
public static void main(String[] args) {
Thread thread1 = new Thread(new Task("Thread 1"));
Thread thread2 = new Thread(new Task("Thread 2"));
thread1.start();
thread2.start();
}
}
In this example, we create a Task
class that implements Runnable
. We then create two threads (thread1
and thread2
), each executing the same task but with different thread names. Each thread counts from 1 to 5 with a 1-second pause between each count.
Conclusion
In this article, we’ve covered the basics of creating and starting threads in Java. We explored two primary methods: extending the Thread
class and implementing the Runnable
interface. We also looked at how to start threads and provided a practical example of creating multiple threads.
Stay tuned for the next article in this series, where we’ll delve deeper into thread lifecycle, synchronization, and thread-safe operations in Java. Happy coding!