Thread Lifecycle: New Runnable Running Blocked Waiting Terminated

#Post9 In last post(https://lnkd.in/ddzdPfvQ), we learned about selecting optimal number of threads for our process. Common assumption is, a thread is either running or not. In reality, a thread spends most of its life NOT running. To understand multithreading properly, we need to understand the lifecycle of a thread. Let’s break it down. 1. New A thread is created but not started yet. Thread t = new Thread(); At this point, it’s just an object in memory. 2. Runnable Once you call start(), the thread becomes ready to run. It doesn’t mean it is running immediately. It simply means it is waiting for CPU scheduling. 3. Running When the CPU scheduler picks the thread, it starts executing. This is the state where actual work happens. 4. Blocked A thread enters this state when it is trying to acquire a lock, but another thread already holds it. Until the lock is released, the thread cannot proceed. 5. Waiting A thread goes into this state when wait() is called. • It pauses execution indefinitely • Releases the monitor lock • Becomes runnable again only after notify() or notifyAll() 6. Timed Waiting A thread waits for a specific amount of time. Examples: • sleep() • join() • wait(timeout) Important detail: • sleep() does NOT release the lock • wait(timeout) releases the lock 7. Terminated The thread has finished execution. Once terminated, it cannot be started again. Below is a reference diagram of the thread lifecycle. We will revisit this as we go deeper into concepts like start(), wait(), and synchronization Key takeaway A thread is not constantly running. In real systems, most threads are: • Waiting • Blocked • Or ready to run Understanding this helps build the foundation for: • Debugging concurrency issues • Avoiding deadlocks • Writing efficient multithreaded code In the next post, we will look at different ways to create threads in Java. #Java #Multithreading #Concurrency #BackendDevelopment #SoftwareEngineering

  • timeline

To view or add a comment, sign in

Explore content categories