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.
Life cycle of thread in java with diagram
1.New State:
2.Runnable State:
3.Running State:
4.Dead State:
5. Non runnable States:
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:
|
This blog has information on Micro Services Architecture, Spring Boot, Spring Cloud, Java and Interview Questions,Tutorials. Also covers some articles on other technologies.
Tuesday, August 2, 2016
Life cycle of a Thread
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment