Java provides multithreading to parallelize execution of tasks (code) in addition to y'all demand threads to run multiple things inwards parallel e.g. download a file inwards the background in addition to present the progress bar at front-end. There are two ways to do a Thread inwards Java, showtime yesteryear extending java.lang.Thread degree in addition to instant yesteryear implementing the java.lang.Runnable interface. Since interviewer loves comparing based questions, what is the difference betwixt extending thread in addition to implementing Runnable is likewise a popular Java thread question. In this article, I'll enjoin y'all how to reply this interrogation yesteryear explaining the departure betwixt extending the Thread degree in addition to implementing the Runnable interface, in addition to which 1 is the improve agency to do threads In Java. Both approaches bring their pros in addition to cons in addition to in that location is a province of affairs when extending Thread is logical but inwards virtually cases implementing Runnable is the improve option. Let's encounter the difference betwixt extends Thread in addition to implements Runnable inwards Java.
As I said, in that location are ii chief ways to do a thread inwards Java, yesteryear extending Thread degree in addition to overriding run() method or yesteryear implementing Runnable interface in addition to overriding run() method, let's encounter the advantages in addition to disadvantage in addition to differences betwixt these ii approaches.
1) The showtime in addition to virtually of import departure betwixt extending Thread in addition to implementing Runnable comes from the fact that a degree tin sack exclusively extend 1 degree inwards Java. So if y'all extend the Thread degree therefore your degree lose that selection in addition to it cannot extend some other class, but if y'all implement Runnable therefore your Thread degree tin sack withal extend some other degree e.g. Canvas. It's a mutual blueprint inwards Java GUI programming that your degree extends Canvas in addition to implements Runnable, EventListener etc.
2) The instant departure betwixt extends Thread in addition to implements Runnable is that using the Runnable representative to encapsulate the code which should run inwards parallel provides improve reusability. You tin sack transcend that Runnable to whatever other thread or thread pool.
3) The 3rd departure comes from OOP perspective. In case, y'all implement Runnable, both Task in addition to Executor ( a thread which execute the task) are loosely coupled but if y'all extend Thread therefore they are tightly coupled.
4) Another departure betwixt Thread in addition to Runnable comes from the fact that y'all are extending Thread degree simply for run() method but y'all volition larn overhead of all other methods which come upwards from Thread class. So, if your destination is to simply write some code inwards run() method for parallel execution therefore purpose Runnable instead of extending Thread class.
5) The 5th departure betwixt extending Thread in addition to implementing Runnable likewise comes from OOP perspective. In Object oriented programming y'all extend a degree to heighten it, to position some novel features on it. So, if y'all simply desire to reuse the run() method, therefore stick alongside implementing the Runnable interface rather than extending Thread class.
6) It's easier to keep code encapsulated inwards Runnable interface because y'all exclusively demand to brand the alter inwards 1 house but if that code is scattered to a greater extent than or less multiple Thread class, y'all demand to brand the alter at multiple places.
7) Last but non the to the lowest degree departure betwixt extends Thread in addition to implements Runnable is that it's good coding practice to purpose Runnable for the specifying chore every bit y'all tin sack reuse it on Thread every bit good every bit on Executor framework.
Here is a squeamish tabular array of departure betwixt extends Thread in addition to implements Runnable inwards Java:
Extends Thread vs implements Runnable
In lodge to download a file inwards the background in addition to present the progress bar inwards GUI y'all demand ii threads, showtime 1 to download the file in addition to instant 1 to present the progress bar. Even though Java provides Thread degree in addition to multi-threading it's programmer's responsibleness to do in addition to contend threads. Though, JDK v likewise provides Executor framework which tin sack receive got creation in addition to administration of threads but every bit a Java developer, y'all should know how to create, start, stop, and pause thread yesteryear yourself.As I said, in that location are ii chief ways to do a thread inwards Java, yesteryear extending Thread degree in addition to overriding run() method or yesteryear implementing Runnable interface in addition to overriding run() method, let's encounter the advantages in addition to disadvantage in addition to differences betwixt these ii approaches.
1) The showtime in addition to virtually of import departure betwixt extending Thread in addition to implementing Runnable comes from the fact that a degree tin sack exclusively extend 1 degree inwards Java. So if y'all extend the Thread degree therefore your degree lose that selection in addition to it cannot extend some other class, but if y'all implement Runnable therefore your Thread degree tin sack withal extend some other degree e.g. Canvas. It's a mutual blueprint inwards Java GUI programming that your degree extends Canvas in addition to implements Runnable, EventListener etc.
2) The instant departure betwixt extends Thread in addition to implements Runnable is that using the Runnable representative to encapsulate the code which should run inwards parallel provides improve reusability. You tin sack transcend that Runnable to whatever other thread or thread pool.
3) The 3rd departure comes from OOP perspective. In case, y'all implement Runnable, both Task in addition to Executor ( a thread which execute the task) are loosely coupled but if y'all extend Thread therefore they are tightly coupled.
4) Another departure betwixt Thread in addition to Runnable comes from the fact that y'all are extending Thread degree simply for run() method but y'all volition larn overhead of all other methods which come upwards from Thread class. So, if your destination is to simply write some code inwards run() method for parallel execution therefore purpose Runnable instead of extending Thread class.
5) The 5th departure betwixt extending Thread in addition to implementing Runnable likewise comes from OOP perspective. In Object oriented programming y'all extend a degree to heighten it, to position some novel features on it. So, if y'all simply desire to reuse the run() method, therefore stick alongside implementing the Runnable interface rather than extending Thread class.
6) It's easier to keep code encapsulated inwards Runnable interface because y'all exclusively demand to brand the alter inwards 1 house but if that code is scattered to a greater extent than or less multiple Thread class, y'all demand to brand the alter at multiple places.
7) Last but non the to the lowest degree departure betwixt extends Thread in addition to implements Runnable is that it's good coding practice to purpose Runnable for the specifying chore every bit y'all tin sack reuse it on Thread every bit good every bit on Executor framework.
Summary
If y'all extend Thread therefore y'all can't extend some other class, y'all volition tightly distich the chore in addition to runner in addition to maintenance of the code volition last tough, but if y'all implement Runnable therefore y'all tin sack withal extend some other class, chore in addition to runner volition last loosely coupled in addition to maintenance of code volition last easier.Here is a squeamish tabular array of departure betwixt extends Thread in addition to implements Runnable inwards Java:
No comments:
Post a Comment