Discussion thread

I came across this claim that it is better to use the Runnable interface instead of extending the Thread class to create a new thread. If so, why does Java even allow us to extend the Thread class. Why not make it final ?

+4
source share
3 answers

Because the thing you do is almost certainly not a thread. This is a ThingTheDoesSomeUsefulWork , and you want it to do this useful work in a separate thread. Let's say I'm writing a thing that polls a web server every 30 seconds. Is ThingThatPollsWebServer thread? Or is it just a thing that polls a web server.

In addition, creating Runnable simplifies testing, and also gives you the flexibility to run your stuff without using a thread or using an artist structure.

+4
source

You might want to change the behavior of the stream or add additional features. If so, then an extension of the Thread class is most appropriate.

+1
source

Using Threads was an old way (before java 5.0) of use, well Threads :). Take a look at the ExecutorService class.

-2
source

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


All Articles