How to create a thread in Java

๐Ÿ’ก Concept: Creating threads in Java

Java supports thread creation by extending the Thread class or implementing the Runnable interface.

๐Ÿ“˜ Quick Intro

Threads can be created by subclassing Thread and overriding run(), or by implementing Runnable and passing it to a Thread object.

๐Ÿง  Analogy

Think of threads like workers in a factory, each doing their own task either assigned directly or through a common interface.

๐Ÿ”ง Technical Explanation

  • Subclass Thread and override run().
  • Implement Runnable interface and implement run() method.
  • Start a thread by calling start(), which internally calls run().
  • Runnable approach is preferred for better design flexibility.
  • Threads share process memory but execute independently.

๐ŸŽฏ Use Cases

  • โœ… Concurrent tasks in GUI apps.
  • โœ… Background processing in server applications.
  • โœ… Parallel execution to improve performance.

๐Ÿ’ป Java Thread Creation Example


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

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

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

        Thread t2 = new Thread(new MyRunnable());
        t2.start();
    }
}

โ“ Interview Q&A

Q1: What are two ways to create a thread in Java?
A: Extending Thread class or implementing Runnable interface.

Q2: Which approach is preferred?
A: Runnable interface for flexibility.

Q3: What method is called to start a thread?
A: start().

Q4: Can you call run() directly?
A: Yes, but it runs in the current thread.

Q5: Do threads share memory?
A: Yes, process memory is shared.

Q6: What is the main advantage of Runnable?
A: Allows extending other classes.

Q7: What happens if start() is called twice?
A: IllegalThreadStateException.

Q8: Can a thread be restarted?
A: No.

Q9: What is a daemon thread?
A: Background thread that terminates with JVM.

Q10: How to stop a thread safely?
A: Use flags or interrupts.

๐Ÿ“ MCQs

Q1. What are two ways to create a thread in Java?

  • Extending Thread
  • Implementing Runnable
  • Both
  • None

Q2. Which approach is preferred?

  • Thread class
  • Runnable interface
  • Callable interface
  • Executor

Q3. What method starts a thread?

  • run()
  • start()
  • init()
  • execute()

Q4. Can you call run() directly?

  • No
  • Yes but runs in current thread
  • Yes and starts new thread
  • Never

Q5. Do threads share memory?

  • No
  • Yes
  • Sometimes
  • Depends

Q6. Main advantage of Runnable?

  • None
  • Allows extending other classes
  • Faster
  • Simpler

Q7. What if start() called twice?

  • No effect
  • IllegalThreadStateException
  • Crash
  • Ignore

Q8. Can thread be restarted?

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

Q9. What is daemon thread?

  • User thread
  • Daemon thread
  • Main thread
  • System thread

Q10. How to stop thread safely?

  • stop()
  • kill()
  • Use flags or interrupts
  • None

๐Ÿ’ก Bonus Insight

Choosing between Thread and Runnable affects flexibility and design in multi-threaded Java applications.

๐Ÿ“„ PDF Download

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

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: