Questions
ThreadBeginner3 min
Difference between Thread and Runnable?
ThreadRunnable
Answer
Thread vs Runnable
Both are ways to define work that runs on a separate thread, but they differ in design and flexibility.
Thread (Extending the Class)
class MyThread : Thread() { override fun run() { println("Running on: ${currentThread().name}") } } // Usage val thread = MyThread() thread.start()
Runnable (Implementing the Interface)
class MyRunnable : Runnable { override fun run() { println("Running on: ${Thread.currentThread().name}") } } // Usage val thread = Thread(MyRunnable()) thread.start() // Or with lambda Thread { println("Lambda runnable") }.start()
Key Differences
| Aspect | Thread | Runnable |
|---|---|---|
| Type | Class (extends) | Interface (implements) |
| Inheritance | Can't extend another class | Can extend another class |
| Reusability | Less reusable | More reusable |
| Resource sharing | Each thread = separate object | Same Runnable, multiple threads |
| Thread pool compatible | No | Yes (Executor.execute(runnable)) |
Why Runnable is Preferred
1. Better OOP Design
class MyActivity : Activity(), Runnable { // Can implement Runnable AND extend Activity override fun run() { /* background work */ } }
2. Separation of Concerns
- Runnable = What to do (task)
- Thread = How to run it (execution)
3. Thread Pool Compatibility
val runnable = Runnable { /* work */ } executorService.execute(runnable) // Works! executorService.execute(thread) // Doesn't make sense
4. Resource Efficiency
val task = Runnable { /* work */ } // Reuse same task logic with multiple threads repeat(5) { Thread(task).start() }
Modern Alternative: Callable
interface Callable<V> { fun call(): V // Can return a result and throw exceptions } val callable = Callable { "Result" } val future = executor.submit(callable) val result = future.get() // "Result"
Best Practice
In modern Android development:
- Use Runnable for simple tasks
- Use Callable when you need results
- Prefer Coroutines for async work (most recommended)
1:1 Mentorship
Get personalized guidance from a Google Developer Expert. Accelerate your career with dedicated support.
Personalized Learning Path
Mock Interviews & Feedback
Resume & Career Guidance
Share & Help Others
Help fellow developers prepare for interviews
Sharing helps the Android community grow 💚