Runnable Interface
The Runnable interface is the preferred and most widely used way to create threads in Java. It represents a task to be executed by a thread, enabling clean design, better flexibility, and scalability. This is a very high-frequency interview topic.
What Is the Runnable Interface?
Runnable is a functional interface that represents a unit of work to be executed by a thread.
@FunctionalInterface
public interface Runnable {
void run();
}
Why Runnable Is Preferred Over Thread
- Supports multiple inheritance
- Separates task (what to do) from thread (how to run)
- Enables better object-oriented design
- Used by Executor framework
- Easier testing and reuse
Creating a Thread Using Runnable
Step-by-Step
- Implement Runnable
- Override
run() - Pass Runnable object to Thread
- Call
start()
Example
class MyTask implements Runnable {
@Override
public void run() {
System.out.println(
"Running in thread: " + Thread.currentThread().getName()
);
}
}
public class Test {
public static void main(String[] args) {
Thread t = new Thread(new MyTask());
t.start();
}
}
- ✔ New thread created
- ✔
run()executed by JVM
Using Runnable with Lambda (Java 8+)
Because Runnable is a functional interface:
Thread t = new Thread(() -> {
System.out.println("Thread using lambda");
});
t.start();
- ✔ Clean
- ✔ Concise
- ✔ Modern
run() vs start() (Critical Interview Point)
t.run(); // ❌ no new thread
t.start(); // ✔ new thread
| Method | Behavior |
|---|---|
run() |
Normal method call |
start() |
Creates new thread |
Runnable and Thread Relationship
Runnable (Task)
↓
Thread (Executor)
- ✔ Runnable contains logic
- ✔ Thread executes logic
Multiple Threads Sharing Same Runnable
Runnable task = new MyTask();
Thread t1 = new Thread(task);
Thread t2 = new Thread(task);
t1.start();
t2.start();
- ✔ Same task
- ✔ Multiple threads
- ⚠ Requires synchronization for shared data
Runnable vs Thread (Interview Table)
| Aspect | Runnable | Thread |
|---|---|---|
| Type | Interface | Class |
| Inheritance | Supports | Does not |
| Code separation | ✔ Yes | ❌ No |
| Reusability | High | Low |
| Recommended | ✔ Yes | ❌ Less |
Runnable vs Callable (Preview)
| Feature | Runnable | Callable |
|---|---|---|
| Return value | ❌ No | ✔ Yes |
| Checked exception | ❌ No | ✔ Yes |
| Used with Executor | ✔ Yes | ✔ Yes |
Common Beginner Mistakes
- Calling
run()directly - Extending Thread unnecessarily
- Sharing mutable state without synchronization
- Ignoring thread safety
- Forgetting thread naming
Best Practices
- Prefer Runnable for thread creation
- Use ExecutorService for production code
- Keep
run()short and focused - Avoid heavy logic inside
run() - Handle exceptions inside
run()
Interview-Ready Answers
Short Answer
Runnable is an interface used to define a task that can be executed by a thread.
Detailed Answer
In Java, the Runnable interface represents a unit of work to be executed by a thread. It defines a single
run() method and is preferred over extending Thread because it supports multiple inheritance,
improves design, and is widely used in modern concurrency frameworks.
Key Takeaway
Runnable defines the task.
Thread runs the task.
Understanding Runnable is essential for scalable, maintainable, and testable Java multithreading.