While reading the article about multithreading in Java, I came across the following lines:

It is believed that the inheritance of the class Thread should be used only when it is really necessary to create a “new kind of thread that should complement the functionality of the class java.lang.Thread”, and this solution is used in developing system software, for example, application servers or infrastructures.

In what cases it may be necessary to supplement the functionality of the class Thread? I would like to see a practical example.

  • you have the answer. An example is the Tomcat application server. in most cases, it is not necessary to inherit. - Mikhail Vaysman
  • one
    The only case when I see the rationality of redefining a thread is to display additional information that the thread is processing, but in this case, as a rule, a fairly competent change of the thread name (eg scanner:worker-0 [idle] -> scanner:worker-0 [webhook execution] -> scanner:worker-0 [idle] ) - etki

1 answer 1

In JavaFx, interface components are updated in the JavaFx stream, i.e. at any time, you can check whether this code is running in a JavaFx stream or not. I do not presume to say that this mechanism is implemented through the inheritance of threads, but it could be implemented in this way. In the case of JavaFx, developers are thus prohibited in advance from doing anything outside the JavaFx stream to prevent asynchronous changes to the interface components.

Using your own threads is good for those cases where you want to prohibit certain tasks in some third-party threads, while imposing additional restrictions and functionality on your threads.

Inheriting Thread can be useful when you want to keep track of your threads, you can create a new thread class that will register new threads even at the constructor stage. Or if you need a special type of synchronization, in which threads will not run on any event, for example, the limit on the number of concurrent threads.

On the other hand, almost all thread-related tasks can be implemented without inheriting, but using the existing tools of the java.util.concurrent package. Multi-threaded programming is a rather difficult task, so it’s better to use ready-made components and solutions without writing your own.