Runnable & Thread
Java Concurrency
Learn how Java runs multiple tasks simultaneously using the Thread class and the Runnable interface: the foundation of concurrent programming.
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.
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.
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
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
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()
Why Runnable Matters
Common Mistakes
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
Consider this code:
Thread t = new Thread(() -> System.out.println("A"));
t.start();
t.start();
System.out.println("B");
What is the result of running this code?