Difference between Runnable and Thread class in Java

๐Ÿ’ก Concept: Runnable vs Thread

Runnable is a functional interface representing a task to be executed, whereas Thread is a class that executes Runnable tasks.

๐Ÿ“˜ Quick Intro

Implement Runnable to define the task and pass it to a Thread instance to execute concurrently.

๐Ÿง  Analogy

Think of Runnable as a recipe and Thread as the chef executing that recipe.

๐Ÿ”ง Technical Explanation

  • Runnable defines a single method run() for the task.
  • Thread class represents a thread of execution.
  • Implementing Runnable is preferred for flexibility (allows inheritance).
  • Thread class can be directly subclassed but limits inheritance.
  • Runnable separates task from execution.

๐ŸŽฏ Use Cases

  • โœ… Use Runnable for better object-oriented design.
  • โœ… Use Thread subclassing for simple cases.
  • โœ… Combine Runnable with Executor framework for advanced concurrency.

๐Ÿ’ป Runnable vs Thread Example


// Using Runnable
class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Runnable running");
    }
}

// Using Thread subclass
class MyThread extends Thread {
    public void run() {
        System.out.println("Thread running");
    }
}

public class Demo {
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnable());
        t1.start();

        MyThread t2 = new MyThread();
        t2.start();
    }
}

โ“ Interview Q&A

Q1: What is Runnable in Java?
A: Functional interface with run() method.

Q2: Can Runnable be used without Thread?
A: No, Runnable must be executed by a Thread.

Q3: Which is preferred: Runnable or Thread?
A: Runnable for flexibility.

Q4: Can Thread extend other classes?
A: No, Thread class is final in some implementations.

Q5: Does Runnable separate task and execution?
A: Yes.

Q6: Can multiple Threads run the same Runnable?
A: Yes.

Q7: What happens if Thread is subclassed?
A: Less flexible design.

Q8: Is Runnable a class or interface?
A: Interface.

Q9: How to execute Runnable?
A: Pass to Thread and call start().

Q10: Can Thread be used without Runnable?
A: Yes, by overriding run().

๐Ÿ“ MCQs

Q1. What is Runnable in Java?

  • Class
  • Interface
  • Functional interface with run() method
  • Abstract class

Q2. Can Runnable be used without Thread?

  • Yes
  • No
  • Sometimes
  • Only in Java 8+

Q3. Which is preferred: Runnable or Thread?

  • Runnable
  • Thread
  • Both
  • None

Q4. Can Thread extend other classes?

  • Yes
  • No
  • Sometimes
  • Only in Java 9+

Q5. Does Runnable separate task and execution?

  • No
  • Yes
  • Sometimes
  • No difference

Q6. Can multiple Threads run the same Runnable?

  • Yes
  • No
  • Sometimes
  • Only one

Q7. What happens if Thread is subclassed?

  • More flexible
  • Less flexible design
  • No effect
  • None

Q8. Is Runnable a class or interface?

  • Class
  • Interface
  • Abstract class
  • Object

Q9. How to execute Runnable?

  • Call run()
  • Pass to Thread and call start()
  • Call main()
  • Use ExecutorService

Q10. Can Thread be used without Runnable?

  • Yes
  • No
  • Sometimes
  • Only in Java 11+

๐Ÿ’ก Bonus Insight

Using Runnable promotes better design by separating task definition from thread execution.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

Learn More About Java โ˜•

What is Java and its key features ๐Ÿ‘‰ Explained
Explain the Java Virtual Machine (JVM) ๐Ÿ‘‰ Explained
Difference between JDK, JRE, and JVM ๐Ÿ‘‰ Explained
What are Javaโ€™s main data types ๐Ÿ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java ๐Ÿ‘‰ Explained
What is the difference between a class and an object ๐Ÿ‘‰ Explained
Explain encapsulation with an example ๐Ÿ‘‰ Explained
What is inheritance in Java and its types ๐Ÿ‘‰ Explained
Define polymorphism in Java with examples ๐Ÿ‘‰ Explained
What is abstraction in Java ๐Ÿ‘‰ Explained
Difference between abstract class and interface in Java ๐Ÿ‘‰ Explained
Explain method overloading and method overriding in Java ๐Ÿ‘‰ Explained
What are constructors in Java ๐Ÿ‘‰ Explained
What is the use of the static keyword in Java ๐Ÿ‘‰ Explained
Explain the difference between final, finally, and finalize in Java ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup