0
0
JavaHow-ToBeginner · 3 min read

How to Create Thread Using Runnable Interface in Java

To create a thread using the Runnable interface in Java, implement the Runnable interface in a class and override its run() method. Then, create a Thread object passing the Runnable instance and call start() on the thread to run it.
📐

Syntax

To create a thread using the Runnable interface, follow these steps:

  • Implement the Runnable interface in a class.
  • Override the run() method with the code you want the thread to execute.
  • Create a Thread object, passing the Runnable instance to its constructor.
  • Call start() on the Thread object to begin execution.
java
class MyRunnable implements Runnable {
    @Override
    public void run() {
        // Code to run in new thread
    }
}

MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();
💻

Example

This example shows how to create a thread by implementing Runnable. The thread prints numbers from 1 to 5 with a short pause.

java
class NumberPrinter implements Runnable {
    @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) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

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

Common Pitfalls

Common mistakes when using Runnable to create threads include:

  • Calling run() directly instead of start(). This runs the code in the current thread, not a new one.
  • Not overriding the run() method, so the thread does nothing.
  • Ignoring InterruptedException when using Thread.sleep().
java
class WrongRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("Running in thread");
    }
}

public class Test {
    public static void main(String[] args) {
        WrongRunnable runnable = new WrongRunnable();
        // Wrong: calls run() directly, no new thread
        runnable.run();

        // Correct: create thread and start it
        Thread thread = new Thread(runnable);
        thread.start();
    }
}
Output
Running in thread
📊

Quick Reference

Summary tips for creating threads with Runnable:

  • Implement Runnable and override run().
  • Create a Thread with the Runnable instance.
  • Always call start() on the Thread, not run().
  • Handle InterruptedException properly when using sleep().

Key Takeaways

Implement Runnable and override run() to define thread code.
Create a Thread object with the Runnable instance and call start() to run it.
Never call run() directly; always use start() to create a new thread.
Handle InterruptedException when using Thread.sleep() inside run().
Runnable allows separating thread code from thread control.