Showing posts with label thread in java. Show all posts
Showing posts with label thread in java. Show all posts

Sunday, 23 November 2014

Life cycle of a thread in java

Life cycle of a thread in java
Life cycle of a thread contains following stages :
New
Runnable
Running
Blocked or Non running
Terminated

 Release 5.0 introduced the Thread.getState method. When called on a thread, one of the following Thread.State values is returned:
  • NEW
  • RUNNABLE
  • BLOCKED
  • WAITING
  • TIMED_WAITING
  • TERMINATED

New : State of Thread before start() method .

Runnable : A thread is in running state after invocation of start() method but has not yet selected by scheduler .

Running : After Scheduler select the thread to run and run() method starts executing then that thread it comes in running state.

Blocked or NonRunning : when a thread goes into wait() or sleep state then its called to be in non running or blocked .It comes into runnable state only if notify or notifyAll() is called on that thread.

Terminated : after a thread is being stopped by stop () method ,it comes into terminated state and can never go back to runnable or running state .



Read More »

Sunday, 7 September 2014

How to Create Threads in Java by Implementing Runnable Interface


                                                     


1.  Let your class implement “Runnable” interface.

2.  Now override the “public void run()” method and write your logic there (This is the method which will be executed when this thread is started).

That’s it, now you can start this thread as given below

1. Create an object of the above class

2.  Allocate a thread object for our thread

3.  Call the method “start” on the allocated thread object.

First Thread

public class FirstThread implements Runnable 
{   
  
  public void run()     
  {                 
    
    for ( int i=1; i<=10; i++)       
    {           
        //Displaying the numbers from this thread           
        System.out.println( "Messag from First Thread : " +i);                      
  
       /*taking a delay of one second before displaying next number               
        *             
        * "Thread.sleep(1000);" - when this statement is executed,            
        * this thread will sleep for 1000 milliseconds (1 second)            
        * before executing the next statement.            
        *             
        * Since we are making this thread to sleep for one second,            
        * we need to handle "InterruptedException". Our thread            
        * may throw this exception if it is interrupted while it              
        * is sleeping.            
        *             
        */         
        try            
        {               
           Thread.sleep (1000);             
        }           
        catch (InterruptedException interruptedException)           
        {               
           /*Interrupted exception will be thrown when a sleeping or waiting 
            *thread is interrupted.                   
            */             
            System.out.println( "First Thread is interrupted when it is sleeping" +interruptedException);           
        }   
    }   
  } 
}  

Second Thread


public class SecondThread implements Runnable 
{   
   //This method will be executed when this thread is executed  
   public void run()    
   {            
      //Looping from 1 to 10 to display numbers from 1 to 10        
      for ( int i=1; i<=10; i++)         
      {             
         System.out.println( "Messag from Second Thread : " +i);                        
         
        /*taking a delay of one second before displaying next number              
         *            
         * "Thread.sleep(1000);" - when this statement is executed,               
         * this thread will sleep for 1000 milliseconds (1 second)            
         * before executing the next statement.               
         *           
         * Since we are making this thread to sleep for one second,               
         * we need to handle "InterruptedException". Our thread               
         * may throw this exception if it is interrupted while it             
         * is sleeping.               
         */            
         try           
         {              
             Thread.sleep(1000);            
         }          
         catch (InterruptedException interruptedException)          
         {              
            /*Interrupted exception will be thrown when a sleeping or waiting                 
             * thread is interrupted.                 
             */                
             System.out.println( "Second Thread is interrupted when it is sleeping" +interruptedException);             
         }      
      }     
    }  
} 


MAIN CLASS

public class ThreadDemo
{
     public static void main(String args[])
     {
        //Creating an object of the first thread
        FirstThread   firstThread = new FirstThread();
      
        //Creating an object of the Second thread
        SecondThread   secondThread = new SecondThread();
      
        //Starting the first thread
        Thread thread1 = new Thread(firstThread);
        thread1.start();
      
        //Starting the second thread
        Thread thread2 = new Thread(secondThread);
        thread2.start();
     }
}


OUTPUT:

Output of ThreadDemo

Read More »