Explain thread lifecycle in Java

๐Ÿ’ก Concept: Thread Lifecycle

The thread lifecycle in Java consists of several states that a thread passes through from creation to termination.

๐Ÿ“˜ Quick Intro

Java threads transition through states like New, Runnable, Running, Waiting, Timed Waiting, and Terminated.

๐Ÿง  Analogy

Think of a thread as a task moving through stages: waiting to start, executing, paused, and finished.

๐Ÿ”ง Technical Explanation

  • New: Thread object created but not started.
  • Runnable: Thread is ready to run and waiting for CPU.
  • Running: Thread is actively executing.
  • Waiting: Thread waiting indefinitely for a signal.
  • Timed Waiting: Thread waits for a specified time.
  • Terminated: Thread has completed execution.

๐ŸŽฏ Use Cases

  • โœ… Understanding thread states aids debugging and optimization.
  • โœ… Helps in managing concurrency effectively.
  • โœ… Useful for designing responsive multi-threaded applications.

๐Ÿ’ป Thread Lifecycle Example


public class ThreadStateDemo {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            System.out.println("Thread Running");
        });
        System.out.println("State after creation: " + t.getState());
        t.start();
        System.out.println("State after start: " + t.getState());
        t.join();
        System.out.println("State after completion: " + t.getState());
    }
}

โ“ Interview Q&A

Q1: What are the thread states in Java?
A: New, Runnable, Running, Waiting, Timed Waiting, Terminated.

Q2: What state is a thread in before start()?
A: New.

Q3: How to check thread state?
A: Using getState() method.

Q4: Can a thread move from waiting to runnable?
A: Yes, when notified.

Q5: What is terminated state?
A: Thread completed execution.

Q6: Difference between waiting and timed waiting?
A: Waiting is indefinite; timed waiting has timeout.

Q7: What method causes timed waiting?
A: sleep(), wait(timeout).

Q8: Can thread be restarted?
A: No.

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

Q10: How to ensure thread safety during state transitions?
A: Use synchronization.

๐Ÿ“ MCQs

Q1. What are the thread states in Java?

  • New only
  • Running only
  • New, Runnable, Running, Waiting, Timed Waiting, Terminated
  • None

Q2. What state is a thread in before start()?

  • Runnable
  • New
  • Running
  • Terminated

Q3. How to check thread state?

  • Using isAlive()
  • Using getState() method
  • Using start()
  • Using run()

Q4. Can a thread move from waiting to runnable?

  • No
  • Yes
  • Sometimes
  • Never

Q5. What is terminated state?

  • Running
  • Waiting
  • Terminated
  • New

Q6. Difference between waiting and timed waiting?

  • Same
  • Waiting is indefinite; timed waiting has timeout
  • Only for daemon threads
  • None

Q7. What method causes timed waiting?

  • start()
  • sleep(), wait(timeout)
  • join()
  • notify()

Q8. Can thread be restarted?

  • Yes
  • No
  • Sometimes
  • Only with Executors

Q9. What happens if start() called twice?

  • Nothing
  • IllegalThreadStateException
  • RuntimeException
  • Error

Q10. How to ensure thread safety during state transitions?

  • Ignore
  • Use synchronization
  • Use wait()
  • Use notify()

๐Ÿ’ก Bonus Insight

Understanding thread lifecycle is key to designing robust 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: