Difference between "implements Runnable" and "extends Thread" in Java
1. Inheritance Option: The limitation with "extends Thread" approach is that if you extend Thread, you can not extend anything else . Java does not support multiple inheritance. In reality , you do not need Thread class behavior , because in order to use a thread you need to instantiate one anyway.
On the other hand,
Implementing the Runnable interface gives you the choice to extend any class you like , but still define behavior that will be run by separate thread.
2. Reusability : In "implements Runnable" , we are creating a different Runnable class for a specific behavior job (if the work you want to be done is job). It gives us the freedom to reuse the specific
behavior job whenever required.
"extends Thread" contains both thread and job specific behavior code. Hence once thread completes execution , it can not be restart again.
"extends Thread" is not a good Object Oriented practice.
4. Loosely-coupled : "implements Runnable" makes the code loosely-coupled and easier to read .
Because the code is split into two classes . Thread class for the thread specific code and your Runnable implementation class for your job that should be run by a thread code.
"extends Thread" makes the code tightly coupled . Single class contains the thread code as well as the job that needs to be done by the thread.
5. Functions overhead : "extends Thread" means inheriting all the functions of the Thread class which we may do not need . job can be done easily by Runnable without the Thread class functions overhead.
Source :- http://javahungry.blogspot.com/2015/05/implements-runnable-vs-extends-thread-in-java-example.html
No comments:
Post a Comment