Course contentsShow
← Java
Lesson 2561 of 2,87048. Concurrency FundamentalsFree lesson

The Thread Class: Creating Threads by Extending

Learn how to create threads by extending the Thread class and overriding the run method.

The Thread Class: Creating Threads by Extending

What you'll learn: How to create and run threads in Java by extending the Thread class and overriding its run method.

The Core Idea

One of the simplest ways to create a thread in Java is by extending the Thread class and overriding its run method. Think of it like creating a custom worker: you define what work the worker should do (in the run method), then you tell the worker to start (by calling start).

The Thread class is Java's built-in representation of an execution thread. When you extend it, you're creating a specialized thread that knows exactly what task to perform.

How It Works

Here's the pattern:

class MyThread extends Thread {
    @Override
    public void run() {
        // Define the work this thread will perform
        System.out.println("Thread is running: " + Thread.currentThread().getName());
    }
}

// Usage
MyThread thread = new MyThread();
thread.start(); // Launches the thread, which calls run() in a new execution path

Important: You call start(), not run(). The start() method creates a new thread of execution and then invokes run() on that thread. Calling run() directly would execute it on the current thread—defeating the purpose of concurrency.

Why Extend Thread?

This approach is straightforward when your class's primary purpose is to be a thread. However, since Java only supports single inheritance, extending Thread means you cannot extend any other class—a significant limitation you'll encounter in more complex designs.

Key Takeaway: Creating threads by extending Thread and overriding run() is the most direct approach, but remember to call start() to launch concurrent execution, not run() directly.