Posts

Showing posts from January, 2019

various types of threads in java

Image
In Java, threads can have States. The  Thread.State  enum defines the different states that a Java thread can have. This enum defines the following values – NEW RUNNABLE BLOCKED WAITING TIMED_WAITING TERMINATED In the subsequent sections, I provide a brief overview of these states along with possible transitions between them. States of a Java Thread NEW This is the default state a thread gets when it is first created. RUNNABLE As soon as a thread starts executing, it moves to the RUNNABLE state. Note that a thread that is waiting to acquire a CPU for execution is still in this state. BLOCKED A thread moves to the BLOCKED state as soon as it gets blocked waiting for a monitor lock. This can happen in one of the following two ways – It’s waiting to acquire a lock to enter a synchronised block/method. It’s waiting to reacquire the monitor lock of an object on which it invoked the  Object.wait  method. WAITING A thread moves to this stat...