Fatskills
Practice. Master. Repeat.
Study Guide: Java Multithreading Thread Lifecycle New Runnable Running Blocked Terminated
Source: https://www.fatskills.com/java-programming/chapter/java-multithreading-thread-lifecycle-new-runnable-running-blocked-terminated

Java Multithreading Thread Lifecycle New Runnable Running Blocked Terminated

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~5 min read

What This Is and Why It Matters

The thread lifecycle in Java is a fundamental concept that describes the various states a thread can be in from its creation to its termination. Understanding this lifecycle is crucial for writing efficient, concurrent programs. It's a common topic in Java certification exams and essential for professionals working with multi-threaded applications. Misunderstanding thread states can lead to deadlocks, race conditions, and inefficient resource utilization, causing significant performance issues and bugs. For instance, improperly managing thread states can result in an application freezing or crashing, impacting user experience and system reliability.

Core Knowledge (What You Must Internalize)

  • Thread: A lightweight process within a program (why this matters: fundamental unit of concurrency).
  • New: The thread is created but not yet started (why this matters: initial state before execution).
  • Runnable: The thread is ready to run and waiting for CPU time (why this matters: transition state before actual execution).
  • Running: The thread is currently executing (why this matters: active state where the thread performs its task).
  • Blocked/Waiting: The thread is waiting for a resource or another thread (why this matters: understanding synchronization and resource management).
  • Terminated: The thread has completed its execution (why this matters: final state indicating completion).
  • Thread.start(): Method to start a thread (why this matters: transitions the thread from New to Runnable).
  • Thread.sleep(): Pauses the current thread for a specified time (why this matters: useful for delaying execution).
  • Thread.join(): Waits for a thread to die (why this matters: synchronizing thread completion).

Step‑by‑Step Deep Dive

  1. Create a Thread (New State)
  2. Action: Instantiate a Thread object.
  3. Principle: The thread is in the New state, ready to be started.
  4. Example: Thread t = new Thread(new MyRunnable());
  5. ⚠️ Pitfall: Forgetting to call start() will leave the thread in the New state indefinitely.

  6. Start the Thread (Runnable State)

  7. Action: Call start() on the thread.
  8. Principle: The thread moves to the Runnable state, ready for CPU allocation.
  9. Example: t.start();
  10. ⚠️ Pitfall: Directly calling run() instead of start() will execute the method in the current thread, not a new one.

  11. Execute the Thread (Running State)

  12. Action: The thread is selected by the scheduler and begins execution.
  13. Principle: The thread is actively running its run() method.
  14. Example: The run() method of MyRunnable is executing.
  15. ⚠️ Pitfall: Long-running tasks without breaks can lead to CPU starvation for other threads.

  16. Block the Thread (Blocked/Waiting State)

  17. Action: The thread needs a resource or is waiting for another thread.
  18. Principle: The thread is paused and waiting for a condition to be met.
  19. Example: synchronized(obj) { ... } or t.join();
  20. ⚠️ Pitfall: Infinite waiting can cause deadlocks if resources are not released properly.

  21. Terminate the Thread (Terminated State)

  22. Action: The thread completes its run() method or is interrupted.
  23. Principle: The thread has finished execution and cannot be restarted.
  24. Example: The run() method finishes, or t.interrupt(); is called.
  25. ⚠️ Pitfall: Not handling InterruptedException can leave threads in an inconsistent state.

How Experts Think About This Topic

Experts view the thread lifecycle as a state machine, where each state represents a different phase of a thread's execution. They focus on transitions between states, understanding that proper management of these transitions is key to writing robust, concurrent applications. Instead of memorizing states, they think in terms of thread behavior and resource management.

Common Mistakes (Even Smart People Make)

  1. The mistake: Calling run() instead of start().
  2. Why it's wrong: run() executes in the current thread, not a new one.
  3. How to avoid: Always use start() to initiate a new thread.
  4. Exam trap: Questions that trick you into calling run() directly.

  5. The mistake: Not handling InterruptedException.

  6. Why it's wrong: Can leave threads in an inconsistent state.
  7. How to avoid: Always catch and handle InterruptedException.
  8. Exam trap: Code snippets that omit exception handling.

  9. The mistake: Forgetting to call start().

  10. Why it's wrong: The thread remains in the New state.
  11. How to avoid: Verify that start() is called after thread creation.
  12. Exam trap: Questions that require identifying the thread state.

  13. The mistake: Infinite waiting in Blocked/Waiting state.

  14. Why it's wrong: Can cause deadlocks.
  15. How to avoid: Use timeouts and check for resource availability.
  16. Exam trap: Scenarios involving synchronization and resource contention.

Practice with Real Scenarios

Scenario 1: A thread is created but never started.
Question: What is the state of the thread? Solution: The thread is in the New state.
Answer: New
Why it works: The thread has been instantiated but not started.

Scenario 2: A thread is started and immediately calls sleep(1000).
Question: What is the state of the thread after calling sleep()? Solution: The thread moves to the Blocked/Waiting state.
Answer: Blocked/Waiting
Why it works: sleep() pauses the thread, putting it in a waiting state.

Scenario 3: A thread completes its run() method.
Question: What is the final state of the thread? Solution: The thread transitions to the Terminated state.
Answer: Terminated
Why it works: The thread has finished execution and cannot be restarted.

Quick Reference Card

  • Core rule: A thread moves through New, Runnable, Running, Blocked/Waiting, and Terminated states.
  • Key method: Thread.start()
  • Critical facts: start() initiates the thread, run() executes the task, sleep() pauses the thread.
  • Dangerous pitfall: Calling run() instead of start().
  • Mnemonic: New, Runnable, Running, Blocked, Terminated (NRBRT).

If You're Stuck (Exam or Real Life)

  • Check first: Verify that start() is called after thread creation.
  • Reason from first principles: Think of the thread lifecycle as a state machine.
  • Use estimation: Estimate the time a thread spends in each state to identify bottlenecks.
  • Find the answer: Refer to the Java documentation or trusted online resources.

Related Topics

  • Thread Synchronization: Understanding how threads interact with shared resources.
  • Concurrency Control: Techniques for managing multiple threads efficiently.


ADVERTISEMENT