Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What is the life cycle of Thread ?

Answer Posted / shajin.a.xavier

Ready-to-run
A thread starts its life cycle with a call to start(). For
example

MyThread aThread = new MyThread();
aThread.start();

A call to start() will not immediately start thread's
execution but rather will move it to pool of threads
waiting for their turn to be picked for execution. The
thread scheduler picks one of the ready-to-run threads
based on thread priorities.
Running
The thread code is being actively executed by the
processor. It runs until it is swapped out, becomes
blocked, or voluntarily give up its turn with this static
method
Thread.yield();

Please note that yield() is a static method. Even if it is
called on any thread object, it causes the currently
executing thread to give up the CPU.
Waiting
A call to java.lang.Object's wait() method causes the
current thread object to wait. The thread remains
in "Waiting" state until some another thread invokes notify
() or the notifyAll() method of this object. The current
thread must own this object's monitor for calling the wait
().
Sleeping
Java thread may be forced to sleep (suspended) for some
predefined time.

Thread.sleep(milliseconds);
Thread.sleep(milliseconds, nanoseconds);

Please note that static method sleep() only guarantees that
the thread will sleep for predefined time and be running
some time after the predefined time has been elapsed.
For example, a call to sleep(60) will cause the currently
executing thread to sleep for 60 milliseconds. This thread
will be in ready-to-run state after that. It will be
in "Running" state only when the scheduler will pick it for
execution. Thus we can only say that the thread will run
some time after 60 milliseconds.
Blocked on I/O.
A java thread may enter this state while waiting for data
from the IO device. The thread will move to Ready-to-Run
after I/O condition changes (such as reading a byte of
data).
Blocked on Synchronization.
A java thread may enter this state while waiting for object
lock. The thread will move to Ready-to-Run when a lock is
acquired.
Dead
A java thread may enter this state when it is finished
working. It may also enter this state if the thread is
terminated by an unrecoverable error condition.

Is This Answer Correct ?    32 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What's the difference between comparison done by equals method and == operator?

915


Write a program to print fibonacci series

1074


Why hashcode is used in java?

918


What is Mutex (Mutual Exclusion Object) ?

1079


How does split work in java?

985


What is array list in java?

1023


What is the purpose of sizeof operator?

978


How do you add an element to an arraylist in java?

882


Why is String immutable?

1015


What is the static import?

1045


What is output buffer?

977


What is substring in java?

1127


What is supplier in java?

954


explain copyonwritearraylist and when do we use copyonwritearraylist?

899


What does exp mean in math?

937