Thursday 8 October 2015

Multithreading in java with example

Multithreading is to provide simultaneous execution of two or more parts of a program to maximum  utilize in the CPU time. In this threading program contains two or more parts that can be run concurrently.


Threads are lightweight processes they share the same address space. multiprocessing and multithreading both are used to achieved multitasking. It is mostly used in games, animations.



Threads can be created into two ways:

  1. By extending thread class
  2. By implementing runnable interface










Methods of Threads:

  1. getName( ) - It is used to be obtaining a threads name.
  2. getPriority( ) - It is used to be obtaining a thread's priority.
  3. getAlive( ) - It is used to be obtaining determine if a thread is still running.
  4. start( ) -  It is used to be obtaining start a thread by calling it run() method.
  5. sleep( ) - It is used to be suspend a thread for a period of time.
  6. run( ) - It is used to be entry point of thread.
  7. join( ) - It is used to be wait for a thread to terminate.


For Example

Create a class name is "SimpleThread.java"

/**
 *
 * @author pradeep
 */

class SimpleThread implements Runnable
{  
  public void run()
{  
    System.out.println("Hellow I am Thread Now I am running.");  
  }   
  public static void main(String args[])
{  
     SimpleThread obj=new SimpleThread();  
     Thread th =new Thread(obj);  
     th.start();  
 }  

}


Result: Output of this program is:

run:
Hellow I am Thread Now I am running.

BUILD SUCCESSFUL (total time: 0 seconds)



Another Example Of Thread:


/**
 *
 * @author pradeep
 */
class CountThread implements Runnable
{
   Thread demothread ;
   CountThread()
   { 
      demothread = new Thread(this, "demo runnable thread");
      System.out.println("demo thread created" + demothread);
      demothread.start();
   }
   public void run()
   {
      try
      {
        for (int t=0 ;t<14;t++)
        {
          System.out.println("Printing the CountThread Now! " + t);
          Thread.sleep(1000);
        }
     }
     catch(InterruptedException ex)
     {
        System.out.println("demo thread interrupted Now!");
     }
     System.out.println("demothread run is over Now!" );
   }
}
class RunnableDemoThread
{
    public static void main(String args[])
    {
       CountThread cnt = new CountThread();
       try
       {
          while(cnt.demothread.isAlive())
          {
            System.out.println("Parent thread will be Alive till the Chlid thread is live"); 
            Thread.sleep(2000);
          }
       }
       catch(InterruptedException ex)
       {
          System.out.println("Parent thread interrupted Now!");
       }
       System.out.println("Parent thread run is over Now!" );
    }

}





Result:
 Output of this program is:

run:
demo thread createdThread[demo runnable thread,5,main]
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 0
Printing the CountThread Now! 1
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 2
Printing the CountThread Now! 3
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 4
Printing the CountThread Now! 5
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 6
Printing the CountThread Now! 7
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 8
Printing the CountThread Now! 9
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 10
Printing the CountThread Now! 11
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 12
Printing the CountThread Now! 13
Parent thread will be Alive till the Chlid thread is live
demothread run is over Now!
Parent thread run is over Now!

BUILD SUCCESSFUL (total time: 17 seconds)





Advantage Of Thread:



  1. You can perform many operations  together so that's why your time not waste.
  2. Threads are independent so it is does't any  affect other thread if exception occur.

Disadvantage Of Thread:

  1. Difficulty of writing codes
  2. Difficulty of debugging
  3. Difficulty of testing.





No comments:

Post a Comment