What is the main advantage of extending the Thread class (or when extending Thread instead of implementing runnable)

I'm trying to figure out what are the possible benefits of extending a Thread class?

This is part of another question that I am describing: There are two ways to create threads in Java

  • extending from class Thread
  • implementation of an executable interface

As shown here , there are several advantages to using the runnable interface. My question is, what is the advantage of extending from the Thread class? The only advantage that comes to my mind is that you can go from the Thread class and call it the ThreadExtended class. Then he / she can add more functionality to ThreadExtended (which I don’t know what it can be), and then when he wants to create a thread, instead of propagating from the Thread class, it extends from ThreadExtended.

Are there any advantages to using the Thread class instead of Runnable? Do you know any classes that extend from the Thread class and then ask users to extend from these classes if they want to have multithreading?

public class ThreadExtended extends Thread{ //override some functions || add more functionality to Thread class } public class MyThread extends ThreadExtended{ public void run() { for(int i=0;i<10;i++) { System.out.println("Using ThreadExtended instead of Thread directly"); } } public static void main(String args[]) { MyThread myThread = new MyThread(); myThread.start(); } } 
+6
source share
5 answers

There is rarely a good reason to extend the Thread class. I would suggest that in most cases you end up just throwing all your do-it-yourself logic into the run method.

You definitely need to stick with Runnable. By choosing the Thread extension, you are creating a class hierarchy that is probably pointless and will limit your ability to refactor things in the future. By choosing Runnable for your implementation, you do not require what the implementation line is, and you can use powerful abstractions such as ExecutorService to distract the nuts and bolts of the code. Finally, using an interface rather than extending a class is a good practice!

+7
source

The only reason for the Thread extension is the need to add behavior related to the thread itself, and not to the task being performed by the thread.

For example, if you are implementing a thread pool (which no one else should do, given java.util.concurrent ), you will need to change the behavior of the thread so that (1) it can accept a new job and (2) it returns to the pool. In a very simplified form:

 public void setRunnable(Runnable runnable) { this.runnable = runnable; } public void run() { while (true) { // wait on lock try { this.runnable.run(); } catch (Throwable ex) { // do something with exception } finally { // return to pool } } } 
+5
source

I find the Thread extension clearer if I also configure the thread, for example:

 class FileReaper extends Thread { FileReaper() { setDaemon(true); setName(getClass().getSimpleName()); } @Override public void run() { // do something } } 
+1
source

Simply put, when you extend Thread, which will be the only class from which you will expand!

0
source

There is also a good reason for extending Thread - if you want to create a Looper Thread:

This is a typical Looper stream implementation, using the separation of prepare () and loop () to create an initial handler to communicate with Looper.

  class LooperThread extends Thread { public Handler mHandler; public void run() { Looper.prepare(); mHandler = new Handler() { public void handleMessage(Message msg) { // process incoming messages here } }; Looper.loop(); } } 

UI fields are a Looper Thread, but you can create your own working Looper Thread. To find out how Thread works with Looper , on which its loop() method works, see my recent answer here .

0
source

Source: https://habr.com/ru/post/944733/


All Articles