Tuesday, August 2, 2016

Life cycle of a Thread


A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in java new, runnable, non-runnable and terminated. There is no running state.
But for better understanding the threads, we are explaining it in the 5 states.

Thread having below states.
  1. New State
  2. Ready State
  3. Running State
  4. Dead State
  5. Non Runnable States 
Life cycle of thread in java with diagram 


1.New State:
  • A thread has been created but not started yet. A thread will be started by calling its start() method.
2.Runnable State:
  • This state is also called ready to run stage also called queue. A thread starts in runnable state by calling start() method.
  • The Thread scheduler decides which thread runs and how long.
3.Running State:
  • If a Thread is executing that means Thread is in Running stage.
4.Dead State:
  • Once a Thread reached dead state it cannot run again.
 5. Non runnable States:
  • A Running Thread transit to one of the non-runnable states, depending upon the circumstances.
  • A Thread remains non runnable until a special transition occurs.
  • A Thread does not go directly to the running state from non-runnable state.
  • But transits first to runnable state.
  1. Sleeping: The Thread sleeps for specified amount of time.
  2. Blocked for I/O: The Thread waits for a blocking operation to complete.
  3. Blocked for join completion: The Thread waits for completion of another Thread.
  4. Waiting for notification: The Thread waits for notification another Thread.
  5. Blocked for lock acquisition: The Thread waits to acquire the lock of an object.
  • JVM executes the Thread based on their priority and scheduling.
Thread Scheduler:
Thread scheduler in java is the part of the JVM that decides which thread should run.
There is no guarantee that which runnable thread will be chosen to run by the thread scheduler.
Only one thread at a time can run in a single process.
The thread scheduler mainly uses preemptive or time slicing scheduling to schedule the threads.

Difference between preemptive scheduling and time slicing
Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence.
Under time slicing, a task executes for a predefined slice of time and then re-enters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors

Thread priority:
Each thread has a priority. Priorities are represented by a number between 1 and 10. In most cases, thread scheduler schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.
3 constants defined in Thread class:
  1. public static int MIN_PRIORITY
  2. public static int NORM_PRIORITY
  3. public static int MAX_PRIORITY
Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10

No comments:

Post a Comment