7 differences betwixt extends Thread in addition to implements Runnable inward Java

Java provides multithreading to parallelize execution of tasks (code) as well as yous demand threads to run multiple things inwards parallel e.g. download a file inwards the background as well as present the progress bar at front-end. There are two ways to do a Thread inwards Java, starting fourth dimension past times extending java.lang.Thread flat as well as minute past times implementing the java.lang.Runnable interface. Since interviewer loves comparing based questions, what is the  difference betwixt extending thread as well as implementing Runnable is too a popular Java thread question. In this article, I'll say yous how to respond this enquiry past times explaining the divergence betwixt extending the Thread flat as well as implementing the Runnable interface, as well as which 1 is the ameliorate agency to do threads In Java. Both approaches convey their pros as well as cons as well as at that spot is a province of affairs when extending Thread is logical but inwards most cases implementing Runnable is the ameliorate option. Let's run into the difference betwixt extends Thread as well as implements Runnable inwards Java.



Extends Thread vs implements Runnable

In social club to download a file inwards the background as well as present the progress bar inwards GUI yous demand ii threads, starting fourth dimension 1 to download the file as well as minute 1 to present the progress bar. Even though Java provides Thread flat as well as multi-threading it's programmer's responsibleness to do as well as grapple threads. Though, JDK five too provides Executor framework which tin handgrip creation as well as management of threads but every bit a Java developer, yous should know how to create, start, stop, and pause thread past times yourself.

As I said, at that spot are ii principal ways to do a thread inwards Java, past times extending Thread flat as well as overriding run() method or past times implementing Runnable interface as well as overriding run() method, let's run into the advantages as well as disadvantage as well as differences betwixt these ii approaches.


1) The starting fourth dimension as well as most of import divergence betwixt extending Thread as well as implementing Runnable comes from the fact that a flat tin solely extend 1 flat inwards Java. So if yous extend the Thread flat as well as then your flat lose that selection as well as it cannot extend some other class, but if yous implement Runnable as well as then your Thread flat tin even thus extend some other flat e.g. Canvas. It's a mutual designing inwards Java GUI programming that your flat extends Canvas as well as implements Runnable, EventListener etc.


2) The minute divergence betwixt extends Thread as well as implements Runnable is that using the Runnable illustration to encapsulate the code which should run inwards parallel provides ameliorate reusability. You tin overstep that Runnable to whatever other thread or thread pool.


3) The 3rd divergence comes from OOP perspective. In case, yous implement Runnable, both Task as well as Executor ( a thread which execute the task) are loosely coupled but if yous extend Thread as well as then they are tightly coupled.


4) Another divergence betwixt Thread as well as Runnable comes from the fact that yous are extending Thread flat merely for run() method but yous volition larn overhead of all other methods which come upwardly from Thread class. So, if your finish is to merely write some code inwards run() method for parallel execution as well as then purpose Runnable instead of extending Thread class.


5) The 5th divergence betwixt extending Thread as well as implementing Runnable too comes from OOP perspective. In Object oriented programming yous extend a flat to heighten it, to position some novel features on it. So, if yous merely desire to reuse the run() method, as well as then stick amongst implementing the Runnable interface rather than extending Thread class.


6) It's easier to keep code encapsulated inwards Runnable interface because yous solely demand to brand the alter inwards 1 house but if that code is scattered to a greater extent than or less multiple Thread class, yous demand to brand the alter at multiple places.


7) Last but non the to the lowest degree divergence betwixt extends Thread as well as implements Runnable is that it's good coding practice to purpose Runnable for the specifying chore every bit yous tin reuse it on Thread every bit good every bit on Executor framework.



Summary

If yous extend Thread as well as then yous can't extend some other class, yous volition tightly duo the chore as well as runner as well as maintenance of the code volition move tough, but if yous implement Runnable as well as then yous tin even thus extend some other class, chore as well as runner volition move loosely coupled as well as maintenance of code volition move easier.
Here is a prissy tabular array of divergence betwixt extends Thread as well as implements Runnable inwards Java:

 Java provides multithreading to parallelize execution of tasks  vii differences betwixt extends Thread as well as implements Runnable inwards Java


That's all most the divergence betwixt extends Thread as well as implements Runnable inwards Java. You tin clearly run into that implementing Runnable is ameliorate than Thread inwards most of the cases except 1 where yous speedily desire to examination something. Stick amongst best practise as well as encapsulate the code amongst Runnable interface to define a Task which tin move executed amongst threads.


Further Reading
  • Applying Concurrency as well as Multi-threading to Common Java Patterns (book)
  • The divergence betwixt a Thread as well as a Process inwards Java? (answer)
  • How to halt a Thread inwards Java? (answer)
  • How to bring together ii threads inwards Java? (answer)
  • How to interruption a Thread inwards Java? (solution)
  • Java Threads By Scott Oaks as well as Henry Wong (book)
  • Java Concurrency inwards Practice past times Brian Goetz (book)


Subscribe to receive free email updates:

0 Response to "7 differences betwixt extends Thread in addition to implements Runnable inward Java"

Posting Komentar