Tuesday, April 7, 2015

What is THREAD in JAVA ?

Thread Life Cycle


THREADS IN JAVA


Creating threads by constructor

class mythread implements Runnable //creates second thread
{  
     Thread t;
     int i;
     mythread()
     {
         t=new Thread(this,"mythread"); //creates new second thread
          t.start(); //start the thread       
     }    
     public void run()
     {                   
             for(i=0;i<5;i++)
             {
                 System.out.println("child thread " + i);                
             }                  
     }
}

public class Thread2 {     public static void main(String[] args)    {        new mythread();  //create new second thread          }}



EXAMPLE


class mythread implements Runnable
{
 int i ;
 public void run()
 {
   for(i=0;i<5;i++)
   {
     System.out.println("Child thread :" + i);
   }
 }
}

class createthread
{
 public static void main(String[ ] args)
 {
   mythread m1=new mythread();
   Thread th=new Thread(m1,"mythread");
  th.start();
 }
}


sleep() :-

makes currently executing thread to sleep for milliseconds.

Syntax:-

void sleep(long milliseconds);

 class mythread implements Runnable
{ 
     Thread t;     int i;     mythread()     {         t=new Thread(this,"mythread"); //creates new second thread                 t.start(); //start the thread      
     } }



public void run()     {         try         {          
             for(i=0;i<5;i++)             {                 System.out.println("child thread " + “hello world”);                 Thread.sleep(1000);             }         }         catch(Exception e)         {          
         }      
         System.out.println("Exitng child thread");     }  
  
}


public class Thread2 {
   
    public static void main(String[] args)
    {
        new mythread();  //create new second thread
        int i;
        try
         {            
             for(i=0;i<5;i++)
             {
                 System.out.println("Main thread " + i);
                 Thread.sleep(3000);
             }
         }
         catch(Exception e)
         {
            
         }
        
         System.out.println("Exitng Main thread");       
       
    }
}


Thread Life Cycle





isAlive() :-


Returns the value true or false depending on thread is terminated or not.

getname():-

Returns the name of the thread specified.

currentThread() :-

When a program is started thread is created automatically that is main thread and the main thread can be accessed using the current thread method of the main class.

class mythread implements Runnable //creates second thread{         Thread t;     int i;     mythread()     {         t=new Thread(this,"mythread"); //creates new second thread         System.out.println("  Child thread  :  " + t.getName() );System.out.println("Is Thread Alive ? " + t.isAlive());       
         t.start(); //start the threadSystem.out.println("Is Thread Alive ? " + t.isAlive());
              }  
     public void run()     {                 
             for(i=0;i<5;i++)             {                 System.out.println("child thread " + i);         
             }               
     }   
}



public class methods {
    public static void main(String[] args)
    {
       mythread m1= new mythread(); 
        System.out.println("The main thread is :" + Thread.currentThread().getName());
    }
}


Thread priorities

Threads can be assigned priorities.

 —Thread priorities are used by operating system to find out which runnable thread is to be given C.P.U time. — Priority is given from range 1-10.  The threads with high priority number will be given preference over threads with lower priority for execution.

Pre-defined priorities




MIN_PRIORITY =1

NORM_PRIORITY =5

MAX_PRIORITY =10

When thread is created default priority 5 is assigned to the thread.


Methods to get and set priority

int getPriority();

setPriority(int priority_number);



class thread1 implements Runnable{  int i; public void run(){    for(i=0;i<5;i++)    {        System.out.println("child-1 " + i);     
    }}}

class thread2 implements Runnable{  int i; public void run(){    for(i=0;i<5;i++)    {        System.out.println("child-2 " + "hello world");     }}}

class prioritythread{ public static void main(String[ ] args) {    thread1 m1=new thread1();    thread2 m2=new thread2();Thread t=new Thread(m1,"thread1");Thread t1=new Thread(m2,"thread2"); t.setPriority(Thread.NORM_PRIORITY - 2);t1.setPriority(Thread.NORM_PRIORITY + 2);t.start(); t1.start();
}}





Thread Life Cycle

THREAD-JOIN 

It may be required to wait for a particular thread to complete its task before

other process.

When join method of thread class is called on thread object , the control waits

for the thread to complete its task and becomes a dead thread.




class thread1 implements Runnable{  int i;
public void run(){   for(i=0;i<5;i++)    {        System.out.println("child-1 " + i); 
    }
   }
}

class jointhread{ public static void main(String[ ] args) { int i;    thread1 m1=new thread1();    thread2 m2=new thread2();    Thread t=new Thread(m1,"thread1");     t.start();try{t.join();for(i=0;i<5;i++){ System.out.println("hello world"); }}catch(Exception e){}}

}



2 comments: