Thread life cycle in java

Thread  Life Cycle:
1.New
2. Runnable
3.Running
4.Waiting or blocked or sleeping
5.Dead






A Thread can be only in one five states.


1.New:


This is the when therad object has been create.
Thread thread=new Thread(r);
At this stage Thread object only created ,but start method has not been invoked.
In this state thread as not at alive.


2.Runnable:


This state after calling start method on thread.In this state thread is eligible to run ,but scheduler has not selected it to be the
running state.
   Thread can enter this state two ways when start() method is invoked
another thread can also return to the runnable state after either running or coming back from a blocked,waiting,or sleeping state.
when thread is in the runnable state ,it is considered as a alive.
thread.start();


3.Running:


This important state in thread life cycle when thread scheduler select its to be the currently executing process.
There is two ways thread can enter Runnbale state but in this (running )state thread can enter only scheduler choose a thread from the running pool.


4.Waiting or blocked or sleeping:


These are actually looking like three different states but main them is similar that's why three sates are combined to one
in all these three state thread is alive after some times or reviving some notification thread back to runnable state.
example:one thread is calculating total of marks of each student another thread is calculating average of student.
avg calculation thread should start after completion of total thread total thread calculate the student total after reviving notification
average thread calculates average.

The important point is that one thread does not tell another thread to block .
t.sleep(),t.yield();
Some methods may look like they tell another thread to block,but they don't
those are static methods those don't effect on instance 't' instead those defined to always affect the thread that's currently executing.


5.Dead:


A thread is considered as a dead sate when its completes run method.
in this state thread no longer considered as to be alive.
Once thread is dead ,its never be brought back to live.if you invoke start() on dead thread instance you will get run time exception.


Author

Written by Admin

Aliquam molestie ligula vitae nunc lobortis dictum varius tellus porttitor. Suspendisse vehicula diam a ligula malesuada a pellentesque turpis facilisis. Vestibulum a urna elit. Nulla bibendum dolor suscipit tortor euismod eu laoreet odio facilisis.

4 comments:

  1. HI,ill give 5 star to this website it provides the latest bank exam question papers new and last year both.
    http://www.kidsfront.com/govt-jobs-exams/ibps-rrb-cwe.html

    ReplyDelete
  2. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete
  3. Thank you for sharing this information

    ReplyDelete