01 / 21

Runnable & Thread
Java Concurrency

Learn how Java runs multiple tasks simultaneously using the Thread class and the Runnable interface: the foundation of concurrent programming.

Java Concurrency

What Is a Thread?

A thread is an independent path of execution within a program. Every Java application starts with at least one thread: the main thread. When you need to do multiple things at the same time: such as downloading a file while updating the UI: you create additional threads.

main() Main Thread New Thread runs runs Both threads execute concurrently

Two Ways to Create Threads

Java gives you two approaches to define what a thread should do. Both achieve the same result, but they differ in design philosophy and flexibility.

Extend Thread
Create a subclass of Thread and override its run() method. The class itself is a thread.
Implement Runnable
Create a class that implements the Runnable interface and pass it to a Thread. The task is separate from the thread.

The Runnable Interface

Runnable is a functional interface in java.lang with a single abstract method. It represents a task that can be executed by a thread.

@FunctionalInterface
public interface Runnable {
    void run();
}

The Thread Class

Thread is a class in java.lang that represents an actual thread of execution. It can either run its own overridden run(), or execute a Runnable passed to its constructor.

public class Thread implements Runnable {
    public Thread(Runnable target) { ... }
    public void start()  { ... } // creates new OS thread
    public void run()    { ... } // called by start()
}

Thread Lifecycle

NEW start() RUNNABLE scheduled RUNNING done TERMINATED BLOCKED/WAITING wait/sleep notify Thread States in Java A thread moves through these states during its life

Extending Thread vs Implementing Runnable

extends Thread implements Runnable
Inheritance Uses up single inheritance Preserves inheritance
Separation of Concerns Task and thread are coupled Task is separate from thread
Reusability Harder to reuse with pools Works with ExecutorService
Syntax Override run() in subclass Pass to new Thread(r)
Lambda Support ✕ Not functional ✓ Functional interface

How to Create and Start a Thread

1
Define the task: Write a class that implements Runnable and put your logic inside the run() method.
2
Create a Runnable instance: Instantiate your class (or use a lambda) to get a Runnable object.
3
Wrap in a Thread: Pass the Runnable to new Thread(runnable).
4
Call start(): Invoke thread.start() to schedule the thread. The JVM will call run() on a new OS thread.

Example 1: Extending Thread

class Greeter extends Thread {

    @Override
    public void run() {
        System.out.println("Hello from " + getName());
    }
}

public class Demo {
    public static void main(String[] args) {
        Greeter t = new Greeter();
        t.start();  // new thread runs run()
    }
}

Example 2: Implementing Runnable

class CountTask implements Runnable {

    @Override
    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println("Count: " + i);
        }
    }
}

public class Demo {
    public static void main(String[] args) {
        Runnable task = new CountTask();
        Thread t = new Thread(task);
        t.start();
    }
}

Example 3: Lambda Shorthand

Since Runnable is a functional interface (one abstract method), you can use a lambda expression instead of creating a separate class. This is the most concise approach.

public class Demo {
    public static void main(String[] args) {

        Thread t = new Thread(() -> {
            System.out.println("Running in: "
                + Thread.currentThread().getName());
        });

        t.start();
    }
}

Running Multiple Threads

public class MultiDemo {
    public static void main(String[] args) {

        Runnable task = () -> {
            String name = Thread.currentThread().getName();
            for (int i = 0; i < 3; i++) {
                System.out.println(name + ": " + i);
            }
        };

        new Thread(task, "Alpha").start();
        new Thread(task, "Beta").start();
    }
}

start() vs run()

t.start() Main Thread New Thread (run) ✓ True concurrency Two threads execute at the same time t.run() Main Thread run() executes HERE ✕ No new thread! ✕ Sequential only Just a normal method call on the calling thread

Why Runnable Matters

Single Inheritance
Java classes can only extend one parent. Using Runnable keeps that slot free for your domain class.
Separation of Concerns
The task (what to do) is separate from the mechanism (how to run). Cleaner, more testable code.
Thread Pool Ready
Runnables can be submitted to an ExecutorService: the foundation of modern Java concurrency.

Common Mistakes

✕ Calling run() Instead of start()
This executes the task on the current thread. No new thread is created: no concurrency happens.
✕ Calling start() Twice
A thread can only be started once. Calling start() again throws an IllegalThreadStateException.
✕ Ignoring Thread Safety
Shared variables accessed by multiple threads without synchronization cause race conditions and unpredictable bugs.

Quick Reference Summary

Concept Details
Runnable Functional interface with void run()
Thread Class that manages an OS thread of execution
start() Creates a new thread and calls run()
run() Just a normal method: no new thread
Lambda () -> { ... } replaces Runnable class
Best Practice Implement Runnable, not extend Thread

Question 1 of 5

Which method must you call on a Thread object to actually create a new thread of execution?

Question 2 of 5

What is the main advantage of implementing Runnable over extending Thread?

Question 3 of 5

What happens if you call run() directly on a Thread object instead of start()?

Question 4 of 5

Why can you pass a lambda expression to the Thread constructor?

Question 5 of 5

What is the result of running this code?