Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Thread Threads in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Threads in Java

Advertisement
Threads in Java help in multitasking. They can stop or suspend a specific running process and start or resume the suspended processes. This helps in increasing the speed of the processes. In Java programming, Java Virtual Machine (JVM) controls the life-cycle of thread. Different states of Thread during its life-cycle are New State, Ready-to-Run, Running and Dead (Terminated) etc.

Threads in Java help in multitasking. They can stop or suspend a specific running process and start or resume the suspended processes. This helps in increasing the speed of the processes.

In Java programming, Java Virtual Machine (JVM) controls the lifecycle of thread.

Different states of Thread during its lifecycle are:

  1. New State: Before the start() method invocation, thread remains in this state as it has been created but it has not been initialized
  2. Ready-to-Run: After the start() method invocation, the thread enters in this stage and is ready-to-run whenever the processor gives it a go.
  3. Running: The scheduler selects the thread from Ready-to-Run pool and CPU allots the time slot and thread is being executed
  4. Dead (Terminated): The thread has been terminated and it cannot run again

While the thread is in Running state, it can further enter into non-runnable states:

  • Sleeping: A thread here sleeps for a specified amount of time but can return to running state.
  • Blocked for Join Completion: A thread in this state is waiting for the completion of another thread.
  • Blocked for I/O: A thread here is waiting for I/O resource completion and can re-enter into Running state after availability of resources.
  • Waiting for notification: A thread here waits for notification from another thread and once it gets the notification it comes back to Running state.
  • Blocked for lock acquisition: A thread in this state is waiting to acquire the lock of an object.

Methods of Thread

Method  Return Type  Description
 currentThread( )  Thread  Returns an object reference to the thread in which it is invoked.
 getName( )  String  Call the name of the thread
 start( )  void  Starts the thread
 run( )  void  This method starts the execution of a thread
 sleep( )  void  Suspends a thread for a specified time
  isAlive( )  boolean  This method determines whethers a thread is running or not
 activeCount( )  int This method returns the number of active threads in a particular group and subgroups
 interrupt( )  void  This method interrupt the threads on which it is invoked.
 yield( )  void  This method pauses the execution of current thread temporarily and allows other threads to execute.

Example of Threads in Java:

public class Threads{
  public static void main(String[] args){
  Thread th = new Thread();
  System.out.println("Numbers are printing line by line after 5 seconds : ");
  try{
  for(int i = 1;i <= 10;i++)
    {
  System.out.println(i);
  th.sleep(5000);
  }
  }
  catch(InterruptedException e){
    System.out.println("Thread interrupted!");
  e.printStackTrace();
  }
  }
}
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 20, 2013

Related Tutorials

Discuss: Threads in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com