What is the difference between thread class and runnable interface




















The third difference comes from the OOP perspective. In case, you implement Runnable, both Task and Executor a thread that executes the task are loosely coupled but if you extend Thread then they are tightly coupled. Another difference between Thread and Runnable comes from the fact that you are extending Thread class just for the run method but you will get overhead of all other methods which come from Thread class.

So, if your goal is to just write some code in the run method for parallel execution then use Runnable instead of extending Thread class. The fifth difference between extending Thread and implementing Runnable also comes from the OOP perspective. In Object-oriented programming, you extend a class to enhance it, to put some new features on it.

So, if you just want to reuse the run method , then stick with implementing the Runnable interface rather than extending the Thread class. It's easier to maintain code encapsulated in the Runnable interface because you only need to make the change in one place but if that code is scattered around multiple Thread classes, you need to make the change at multiple places.

Best Practice. Last but not least difference between extending Thread and implementing Runnable is that it's good coding practice to use Runnable for the specifying task as you can reuse it on Thread as well as on the Executor framework.

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. Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

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:. November 4, at AM javin paul said November 5, at AM.

Newer Post Older Post Home. Subscribe to: Post Comments Atom. Subscribe for Discounts and Updates Follow.

Search This Blog. Interview Questions core java interview question data structure and algorithm 78 Coding Interview Question 75 interview questions 70 design patterns 35 SQL Interview Questions 34 object oriented programming 34 thread interview questions 30 spring interview questions 28 collections interview questions 25 database interview questions 16 servlet interview questions 15 Programming interview question 6 hibernate interview questions 6.

How to design a vending machine in Java? How HashMap works in Java? Why String is Immutable in Java? Translate This Blog. ClassNotFoundException: org. Law of Demeter in Java - Principle of least Knowle When to Make a Method Static in Java? Is it possible to have an abstract method in a fin Top 5 Courses to learn Groovy and Grails in Why Enum Singleton are better in Java? Difference between repaint and revalidate method i How to Count number of Set bits or 1's of Integer When a class is loaded and initialized in JVM - Ja Is Swing Thread Safe in Java?

How to get current URL, parameters and Hash tag us How ClassLoader Works in Java? Example 3 ways to solve java. NoClassDefFoundError in How to use Comparator and Comparable in Java? How to compare two lists of values in Microsoft Ex How to increase Heap memory of Apache Tomcat Serve What is bounded and unbounded wildcards in Generic How to Split String based on delimiter in Java?

Difference between Right shift and Unsigned right What is the maximum Heap Size of 32 bit or bit Extending In Java, multiple inheritance not allowed hence, after a class extends Thread class, it can not extend any other class. If a class define thread implementing the Runnable interface it has a chance of extending one class. Use A user must extend thread class only if it wants to override the other methods in Thread class. If you only want to specialize run method then implementing Runnable is a better option.

Coupling Extending Thread class introduces tight coupling as the class contains code of Thread class and also the job assigned to the thread Implementing Runnable interface introduces loose coupling as the code of Thread is separate form the job of Threads. Thread is a class in java. The Thread class extends an Object class, and it implements Runnable interfaces. The Thread class has constructors and methods to create and operate on the thread. When we create multiple threads, each thread creates a unique object and get associated with that object.

If you create a thread extending Thread class, further you can not extend any other class as java does not support multiple inheritance. So, you should choose to extend Thread class only when you also want to override some other methods of Thread class. Let us see an example of creating a thread extending a Thread class. In the code above, I create a class Mythread that extends Thread class and overrides a run method of Thread class.

In the class containing the main method I create a thread object mt of Mythread class and using the thread object invoked the start method.



0コメント

  • 1000 / 1000