0
0
JavaHow-ToBeginner · 4 min read

How to Create Thread by Extending Thread Class in Java

To create a thread by extending the Thread class in Java, define a new class that extends Thread and override its run() method with the code you want the thread to execute. Then, create an instance of your class and call its start() method to begin the thread.
📐

Syntax

To create a thread by extending the Thread class, follow these steps:

  • Extend the Thread class in your new class.
  • Override the run() method with the code the thread should execute.
  • Create an instance of your subclass.
  • Call the start() method on the instance to start the thread.
java
class MyThread extends Thread {
    @Override
    public void run() {
        // Code to run in new thread
    }
}

// To start the thread:
MyThread t = new MyThread();
t.start();
💻

Example

This example shows a thread that prints numbers from 1 to 5 with a short pause between each print. It demonstrates how to extend Thread, override run(), and start the thread.

java
class NumberThread extends Thread {
    @Override
    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println("Number: " + i);
            try {
                Thread.sleep(500); // Pause for 0.5 seconds
            } catch (InterruptedException e) {
                System.out.println("Thread interrupted");
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {
        NumberThread thread = new NumberThread();
        thread.start();
    }
}
Output
Number: 1 Number: 2 Number: 3 Number: 4 Number: 5
⚠️

Common Pitfalls

Common mistakes when creating threads by extending Thread include:

  • Calling run() directly instead of start(). Calling run() runs the code in the current thread, not a new thread.
  • Not overriding run(), so the thread does nothing.
  • Ignoring InterruptedException when using sleep() or other blocking calls.
java
class WrongThread extends Thread {
    @Override
    public void run() {
        System.out.println("Running in thread");
    }
}

public class Main {
    public static void main(String[] args) {
        WrongThread t = new WrongThread();
        t.run(); // WRONG: runs in main thread
        t.start(); // CORRECT: runs in new thread
    }
}
Output
Running in thread Running in thread
📊

Quick Reference

Remember these key points when creating threads by extending Thread:

  • Extend Thread and override run().
  • Use start() to begin the thread.
  • Do not call run() directly.
  • Handle InterruptedException properly.
StepDescription
Extend ThreadCreate a class that extends the Thread class.
Override run()Put the code to run in the new thread inside run().
Create instanceMake an object of your subclass.
Call start()Invoke start() to run the thread concurrently.

Key Takeaways

Extend the Thread class and override the run() method to define thread behavior.
Always call start() to launch a new thread, not run().
Handle InterruptedException when using sleep or blocking calls inside run().
Creating threads by extending Thread is simple but limits subclassing other classes.
Use this method for quick thread creation when no other inheritance is needed.