← Back to Multithreading Course

JAVA MULTITHREADING COURSE · CLASS 02

Creating Threads in Java

In our previous class, we understood what multithreading is and why applications need multiple threads.

Today, let's learn how to create a new thread in Java. We will also understand the difference between start() and run().

1. A Quick Recap

Remember our example of workers?

A thread is like a worker who performs a task inside our application.

When a Java application starts, Java creates a thread called the main thread. This thread executes our main() method.

But what if we want another worker to perform a task independently? We need to create another thread.

2. Why Create Another Thread?

Imagine that we are building an application that needs to download a file and display some information to the user.

If we perform the download on the main thread, the main thread has to wait for the download operation to finish before it can continue with the next statement.

Instead, we can assign the download task to another thread.

Now the main thread can continue doing its work while the new thread handles the download.

This is one of the reasons we use multithreading.

3. Our First Thread Using the Thread Class

Java provides a built-in class called Thread. We can extend this class to create our own thread.

Let's look at a simple example.

public class MyFirstThread extends Thread {

    @Override
    public void run() {
        System.out.println("Hello from the new thread!");
    }

    public static void main(String[] args) {

        MyFirstThread thread = new MyFirstThread();

        thread.start();

        System.out.println("Hello from the main thread!");
    }
}

4. Understanding the Code

Step 1: Extend the Thread Class

public class MyFirstThread extends Thread

Here, our class MyFirstThread extends Java's built-in Thread class.

In simple words, we are creating our own class using the functionality provided by Java's Thread class.

Step 2: Write Our Task Inside run()

@Override
public void run() {
    System.out.println("Hello from the new thread!");
}

The run() method contains the task that we want our new thread to perform.

In this example, the task is simply printing a message.

Step 3: Create an Object

MyFirstThread thread = new MyFirstThread();

We create an object of our MyFirstThread class.

At this point, we have created the thread object, but we have not started its execution.

Step 4: Start the Thread

thread.start();

This is where the actual thread execution begins.

When we call start(), Java starts a new thread of execution. That new thread then executes our run() method.

5. What Is the Difference Between start() and run()?

This is probably the most important concept in today's class.

Calling run() Directly

MyFirstThread thread = new MyFirstThread();

thread.run();

When we call run() directly, Java simply executes it like a normal method.

It does not create a new thread.

The code inside run() executes on the same thread that called it.

Calling start()

MyFirstThread thread = new MyFirstThread();

thread.start();

When we call start(), Java starts a new thread, and that new thread executes the run() method.

Remember this simple difference:

Therefore, when we want to create a new thread, we call start(), not run() directly.

6. What Happens When Both Threads Run?

Let's look at our first example again.

public class MyFirstThread extends Thread {

    @Override
    public void run() {
        System.out.println("Hello from the new thread!");
    }

    public static void main(String[] args) {

        MyFirstThread thread = new MyFirstThread();

        thread.start();

        System.out.println("Hello from the main thread!");
    }
}

We now have two threads:

Both threads can make progress independently.

But can we guarantee which message will be printed first? No!

Sometimes the main thread may print first. Sometimes the new thread may print first.

We cannot predict the exact execution order.

7. Try It Yourself

Change the message inside the run() method:

System.out.println("I am downloading a file!");

And change the message inside the main() method:

System.out.println("I am doing other work!");

First, execute the program using start(). Then replace it with run() and execute the program again.

Observe the difference. This small experiment will help you understand today's concept.

8. Test Your Understanding

Let's check what we have learned. Choose one answer for each question.

Question 1: Which Java class can we extend to create a thread?

Question 2: Where do we write the task that a thread should perform?

Question 3: What happens when we call start()?

Question 4: What happens when we call run() directly?

Question 5: Can we guarantee which thread prints its message first?

9. What Did We Learn Today?

Let's quickly revise:

Great job! We have created our first thread in Java.

In the next class, we will learn another way of creating threads using the Runnable interface.

Your Course Progress

0% Completed

You have completed 2 out of 44 classes.