Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Life Cycle of A Thread

                         

When you are programming with threads, understanding the life cycle of thread is very valuable. While a thread is alive, it is in one of several states. By invoking start() method, it doesn’t mean that the thread has access to CPU and start executing straight away. Several factors determine how it will proceed.

Different states of a thread are :

  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.

Different states implementing Multiple-Threads are:

As we have seen different states that may be occur with the single thread. A running thread can enter to any non-runnable state, depending on the circumstances. A thread cannot enters 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.
  • 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
  • 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

         
  • 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.
         
  • Blocked for joint completion – The thread can come on this state because of waiting the completion of another thread.
        
  • Blocked for lock acquisition – The thread can come on this state because of waiting to acquire the lock of an object.

 Methods that can be applied apply on a Thread:

Some Important Methods defined in java.lang.Thread are shown in the table:

 Method  Return Type  Description
 currentThread( )  Thread  Returns an object reference to the thread in which it is invoked.
 getName( )  String  Retrieve the name of the thread object or instance.
 start( )  void  Start the thread by calling its run method.
 run( )  void  This method is the entry point to execute thread, like the main method for applications.
 sleep( )  void  Suspends a thread for a specified amount of time (in milliseconds).
  isAlive( )  boolean  This method is used to determine the thread is running or not.
 activeCount( )  int  This method returns the number of active threads in a particular thread group and all its subgroups.
 interrupt( )  void  The method interrupt the threads on which it is invoked.
 yield( )  void  By invoking this method the current thread pause its execution temporarily and allow other threads to execute.
 join( )  void  This method and  join(long millisec) Throws InterruptedException.  These two methods are invoked on a thread. These are not returned until either the thread has completed or it is timed out respectively.

 

    

     

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (post your own) View All Comments Latest 10 Comments:

this is really very nice website,
please let me know that how can i get familiar with the methods,and how can i get them in mind for ever.

Posted by syed muslim jamil on Saturday, 06.7.08 @ 09:51am | #62420

Thanks Information is very use ful and easy to understand

Posted by Pooja on Thursday, 05.15.08 @ 16:30pm | #60039

thank you,
this diagram is very useful.

Posted by dim on Thursday, 04.17.08 @ 22:45pm | #56976

Hey, Life cycle of thread is usefull for me because after 5 days my exams will be start and i want to give java programming paper..

Thanks.

Posted by Dhaval Panchal on Friday, 12.21.07 @ 15:53pm | #43225

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.