Thursday, October 29, 2009

Thread Life Cycle

A Thread encounters four states during its life time which are:-


1.New Thread(Init)
2.Runnable
3.Waiting/Blocked/Ready
4.Terminated




In order to get to the roots of the threads you have to understand all the states of Threads life cycle.

1.New Thread(Init) :- Whenever an instance of Thread is created it enters the new Thread state.It is not mandatory for you to always extend the Thread class.In order to create a new Thread instance there is also another convenient option which is implementing the Runnable interface.Once a new Thread is created you can start the thread.Consider following example:-

Creating a new Thread

Thread NThread = new Thread(this);
The above expression creates an empty Thread with no resources allocated to it.The NThread can be started by using the start() method on the Thread object.

i.e. NThread.start();

2.Runnable:- When the start() method is invoked,the Thread enters Runnable state.Thread invokes start() method doesn't mean that the Thread will go on to execute straight away.It may begin execution but its not mandatory,it will begin executing when the processor becomes idle.If processor is idle then the Thread will start executing straight away.The processor maintains a queue to know about which Thread is coming next.

NOTE:- Invoking start() method does not guarantee about the execution of the Thread,Thread may begin execution immediately or it may keep you waiting for a long time anything can happen,but the bottom line is "In case of Threads Nothing is Guaranteed" until and unless you make them do guaranteed behaviour.

3.Waiting/Blocked/Ready :- During this state the Threads are remained in a pool .This Thread pool contains Threads which are either blocked by some IO or resource or waiting for their turn.sleep(),wait() and notify() are a few methods which put threads into this state.Once again I will say that the order in which the Threads are waiting does not holds any importance in context to their execution because selection of Thread to execute is totally based on the decision of the underlying OS.That is why nothing is guaranteed in Thread world!!!!


Terminated:- A Thread can be either killed or die naturally .When the thread completes its run() method then it dies naturally,but if you want to kill a Thread the assign null to its object.The best way to check whether the Thread is dead or alive is to use isAlive() method.It returns state dead or alive.

Important:- If a Thread is once dead or terminated then it cannot be restarted.You can also not call the methods of a dead Thread.

NOTE:- " IN THE WORLD OF THREAD NOTHING IS GUARANTEED!!!!!!" but you can assume you will learn the behaviours of Threads.

Suggested Reading:-

Java Thread Basics
Ways of creating Threads

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP