Monday, August 27, 2012

Thread LifeCycle

Life Cycle of Thread :

thread's life cycle consists of five states:


New Thread : When an instance of a thread class is created, a thread enters the new thread state.        Thread newThread = new Thread(this);
                     You have to invoke the  Start( ) to start the thread.  ie, newThread.Start( );

Runnable : when the Start( ) of the thread is invoked the thread enters into the Runnable State.



sleep(long t);    where t= no: of milliseconds for which the thread is inactive.
The sleep( ) is a static method because it operates on the current thread.

Dead : A thread can either die natuarally or be killed.
A thread dies a natural death when the loop in the Run( ) is complete.
Assigning null to the thread Object kills the thread.
If the loop in the Run( ) has a hundred iterations , the life of the thread is a hundred iterators of the loop.

IsAlive( ) : it is used to determine wheather a thread has been started or stopped. If isAlive( ) returns true the thread is still running otherwise running completed.

Thread Priorities : are used by the thread scheduler to decide when each thread should ne allowed to run.To set a thread priority, use te setpriority( ), which is a member of a thread.
     final void setpriority(int level)     - here level specifies the new priority seting for the calling thread.

   The value level must be with in the range :-
 MIN_PRIORITY  is  1
 NORM_PRIORITY is  5
 MAX_PRIORITY  is  10

0 comments:

Post a Comment