Monday, February 23, 2009

The Moody Thread States

Understanding the moods of threads is critical to programming with threads.



1. New state – After the creations of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.

2. Runnable (Ready-to-run) state – A thread start its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting for a turn on the processor.

3. Running state – A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.

4. Dead state – A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again.

5. Blocked - A thread can enter in this state because of waiting the resources that are hold by another thread.



A running thread can enter to any non-runnable state, depending on the circumstances. A thread cannot enter directly to the running state from non-runnable state, firstly it goes to runnable state. Now lets understand the some non-runnable states which may be occur handling the multithreads.

1.Sleeping – On this state, the thread is still alive but it is not runnable, it might be return to runnable state later, if a particular event occurs. On this state a thread sleeps for a specified amount of time. You can use the method sleep( ) to stop the running state of a thread.

static void sleep(long millisecond) throws InterruptedException

2.Waiting for Notification – A thread waits for notification from another thread. The thread sends back to runnable state after sending notification from another thread.

final void wait(long timeout) throws InterruptedException
final void wait(long timeout, int nanos) throws InterruptedException
final void wait() throws InterruptedException

3.Blocked on I/O – The thread waits for completion of blocking operation. A thread can enter on this state because of waiting I/O resource. In that case the thread sends back to runnable state after availability of resources.

4.Blocked for joint completion – The thread can come on this state because of waiting the completion of another thread.

5.Blocked for lock acquisition – The thread can come on this state because of waiting to acquire the lock of an object.

4 comments:

Anonymous said...

free forex signals

Anonymous said...

feast homes
[url=http://vimeo.com/user4498961]prices for hotels
[/url]serviced accommodation
parade and lodging deals
important bed prices
[url=http://www.earthday.org/users/48490]cheap maui hotels
[/url]booking bed reservations
apartments 4 hire
discounted lodgings
[url=http://www.fairview.org/cty/members/klemot/default.aspx]hotel du pantheon
[/url]cheap flights
new control group
airline check
[url=http://www.youthcabinet.org/profile/Josh]great hotel prices
[/url]mould jiffy hotel deals
treaty search
air and hotel deals
[url=http://www.beautyresearch.com/blogs/alexa/archive/2010/08/15/hotels-and-accomodations.aspx]ginger hotel
[/url]tinpot flighs
tight vancouver hotels
bargain on hotels
[url=http://www.mazdacommunity.com/profiles/blogs/special-hotel-offers-or]hotel etoiles
[/url]cheap residence france
grant inn
cheap bed chains

Anonymous said...

bet on sports, bet football, foootball betting
bet online online sports betting betting online

Anonymous said...

[url=http://louboutinshop.co.uk]christian louboutin outlet uk[/url] One species of Somateria, the Eider Duck does. [url=http://dkgoose.com]Canada Goose Parka[/url] Kltyjakmi [url=http://canadagoosesweden.com]Canada goose outlet [/url]
erqxse 992843 [url=http://www.canadagoosestorontofactory.ca]canadian geese nesting[/url] 105857 [url=http://www.officialcanadagooseparkas.ca]canada goose authentic[/url]