Saturday, July 26, 2014

Ways to implement Threads in Java.

You can create thread in 4 ways... 

1- Using anonymous class implementation of Thread. 
2- By Implementing Runnable Interface. 
3- By extending the Thread class itself. 

1- Using anonymous class implementation of Thread:

         Using Anonymous Thread>>> 

         public class CreateMultiThread {
            public static void main(String[] args) {
              Thread t1 = new Thread ("First"){
                     @Override
                     public void run() {
                           for (int i = 0; i < 4; i++) {
                                  System.out.println(getName()+" : "+i);
                           }
                     }
              };
             
              Thread t2 = new Thread ("second"){
                     @Override
                     public void run() {
                           for (int i = 0; i < 4; i++) {
                                  System.out.println(getName()+" : "+i);
                           }
                     }
              };
             
              t1.start();
              t2.start();
              System.out.println("In Main()");
         }
    }

    OUTPUT:
      In Main()
      First : 0
      second : 0
      First : 1
      second : 1
      First : 2
      second : 2
      First : 3
      second : 3

Using Anonymous Runnable>>> 
    
 public class CreateMultiThread {
   public static void main(String[] args) { 
      Runnable runnable= new Runnable() {
   @Override
   public void run() {
    for (int i = 0; i < 4; i++) {
     System.out.println(i);
    }
   }
       };
 
     Thread t1 = new Thread(runnable,"First");
     Thread t2 = new Thread(runnable,"Second");
     t1.start();
     System.out.println("Thread: "+t1.getName());
     t2.start();
     System.out.println("Thread: "+t2.getName());
     System.out.println("In Main()");

    }
}

OUTPUT>>
      Thread: First
        0
        1
        2
        3
        0
        1
        2
        3
        Thread: Second
        In Main()


    2- By Implementing Runnable Interface:
      
        class ThreadUsingRunnable implements Runnable {
 private Thread t;
 private String name;

 ThreadUsingRunnable(String name) {
  this.name = name;
  t = new Thread(this, name);
  System.out.println("Child Thread:: " + t);
  t.start();
               }

 public void run() {
  for (int i = 0; i < 4; i++) {
   System.out.println(name + ": " + i);
  }
 }
     }
     
 //Testing the thread
 public class CreateMultiThread {
 public static void main(String[] args) {
 
  new ThreadUsingRunnable("first");
  new ThreadUsingRunnable("second");
  new ThreadUsingRunnable("third");
  try {
   Thread.sleep(1000);
  } catch (InterruptedException ee) {
   System.out.println("Main Intrrupted!!");
  }
  System.out.println("Main Exiting!!");
 }
   }
   
    OUTPUT>>
     Child Thread:: Thread[first,5,main]
     Child Thread:: Thread[second,5,main]
     first: 0
     first: 1
     Child Thread:: Thread[third,5,main]
     first: 2
     second: 0
     first: 3
     second: 1
     second: 2
     second: 3
      third: 0
      third: 1
      third: 2
      third: 3
      Main Exiting!!
   
    
    3By extending Thread class itself:

       class ThreadUsingThreadClass extends Thread {
 private String name;
 ThreadUsingThreadClass(String name) {
  super(name);
  this.name = name;
  System.out.println("Child Thread:: " + this);
  start();
 }

 public void run() {
  for (int i = 0; i < 4; i++) {
   System.out.println(name + ": " + i);
  }
 }
     }

 //Testing the thread
 public class CreateMultiThread {
      public static void main(String[] args) {
  new ThreadUsingThreadClass("first");
  new ThreadUsingThreadClass("second");
  new ThreadUsingThreadClass("third");
  try {
   Thread.sleep(1000);
  } catch (InterruptedException ee) {
   System.out.println("Main Intrrupted!!");
  }
  System.out.println("Main Exiting!!");
          }
      }
  
     OUTPUT>>
      Child Thread:: Thread[first,5,main]
      Child Thread:: Thread[second,5,main]
      first: 0
      first: 1
      first: 2
      first: 3
     Child Thread:: Thread[third,5,main]
     second: 0
     second: 1
     second: 2
     second: 3
     third: 0
      third: 1
      third: 2
      third: 3
      Main Exiting!!


Which method is best for implementing Threads::

1st Reason>>

Creating your thread implementation class by extending the Thread class makes it heavy, as it inherits all the properties of Thread class.
While implementing Runnable , our motive is to provide the implementation of run() method not the actual Thread class. As we all know Thread class also implement Runnable to get the run() method. Hence the object implementing Runnable will have run () method and will be a lightweight object.


2nd Reason>>

In Java we can extend only one class at a time, so if we extend Thread class we can’t extend any other class while by implementing Runnable interface we still have that option to extend some other class. 

Hence, create your thread implementation by extending the Thread class only when you want to override the some behavior of Thread class.


Leave your comments/suggestions below. Otherwise the next time :)


    


No comments:

Post a Comment

Thanks for your comments/Suggestions.