Tuesday, 3 June 2014

How many ways to create a thread in Java? Which one Prefer and why?

There are two ways to create a thread in Java.

  • Extending the Thread class
  • Using Runnable interface(this one prefer)

  
        One way of creating a thread in Java is to define a class that is a derived(Extending) from the Thread class which is built into Java. An object of this derived class will be a thread.
        The next question is once we derive from the Thread class, where do we actually do the programming for that thread? Well, the Thread class has a method called run, which you must override in order to have your thread do whatever you want it to do. Suppose we have a class called Derived that extends from the Thread class, then it would look like this:

Example of creating a thread by Extending from the Thread class:
     

  class ThreadDemo {

        public static void main(String[] args) {

               MyThread mt = new MyThread();
               mt.start();
               for(int i=0; i<50; i++) {
                     System.out.println("i="+i+",i*i="+i*i);
               }
         }
   }

   class MyThread extends Thread {

            public void run() {
                   for(int count = 1,row = 1;row<20; row++,count++) {
                        for(int i =0; i<count; i++) {                                      
                            System.out.print("*");
                            System.out.print("\n");
                        }
                   } 
            }
    }
     
   Java does not support multiple inheritance, which means that you can not derive from multiple classes at once. So, there may be a situation in which you want to create a thread, but you also want to derive from a class that is not the Thread class. For this reason, Java provides an alternative way of creating a thread – so that you don’t have to create a class that derives from the Thread class. Java provides an interface called Runnable that your class can implement in order to become a thread. The Runnable interface has only one method heading that you must implement yourself:

       public void run();

       If you create a class that implements the Runnable interface then that class must still be run from an instance of the Thread class. You can accomplish this by passing the Runnable object as an argument to the Thread constructor. If we look at some code this will be much clearer. Suppose we want to create a thread by implementing the Runnable interface, then we would have something like the following:

Example of creating a thread by implementing the Runnable interface:  
         

 class ThreadDemo {

       public static void main(String[] args) {

              MyThread mt = new MyThread();

              Thread t = new Thread(mt);

              t.start();

       }

 }


 class MyThread implements Runnable {

          public void run() {
                 System.out.println("Implementing Runnable interface");

          }

  } 
        
 
        You have now seen that in order to create a thread you can either extend from the thread class or implement the Runnable interface. As you've probably noticed , in either case you must implement the "run" method.

       Another interview question,How to handle the thread if to change the return type of  run() method in thread? is there any other thread interface to handle this situation?

         Use callable interface. For more details about Callable interface please Click Here..

No comments:

Post a Comment