Implement Runnable vs Extend Thread in Java

As discussed in Java multi-threading article we can define a thread in the following two ways:

  • By extending Thread class
  • By implementing Runnable interface

In the first approach, Our class always extends Thread class. There is no chance of extending any other class. Hence we are missing Inheritance benefits. In the second approach, while implementing Runnable interface we can extends any other class. Hence we are able to use the benefits of Inheritance.
Because of the above reasons, implementing Runnable interface approach is recommended than extending Thread class.

The significant differences between extending Thread class and implementing Runnable interface:

  • When we extend Thread class, we can’t extend any other class even we require and When we implement Runnable, we can save a space for our class to extend any other class in future or now.
  • When we extend Thread class, each of our thread creates unique object and associate with it. When we implements Runnable, it shares the same object to multiple threads.

Lets have a look on the below programs for better understanding:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate defining Thread
// by extending Thread class
  
// Here we cant extends any other class
class Test extends Thread 
{
    public void run()
    {
        System.out.println("Run method executed by child Thread");
    }
    public static void main(String[] args)
    {
        Test t = new Test();
        t.start();
        System.out.println("Main method executed by main thread");
    }
}

chevron_right


Output:

Main method executed by main thread
Run method executed by child Thread
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate defining Thread
// by implements Runnable interface
class Geeks {
    public static void m1()
    {
        System.out.println("Hello Visitors");
    }
}
  
// Here we can extends any other class
class Test extends Geeks implements Runnable {
    public void run()
    {
        System.out.println("Run method executed by child Thread");
    }
    public static void main(String[] args)
    {
        Test t = new Test();
        t.m1();
        Thread t1 = new Thread(t);
        t1.start();
        System.out.println("Main method executed by main thread");
    }
}

chevron_right


Output:

Hello Visitors
Main method executed by main thread
Run method executed by child Thread

NOTE : In the case of multithreading we cant predict the exact order of output because it will vary from system to system or JVM to JVM.

Reference: https://stackoverflow.com/questions/15471432/why-implements-runnable-is-preferred-over-extends-thread

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.