I know that implements Runnable
is preferred over extends Thread
in Java threads as it allows us to extend some other class if it is required. But if this is the case, does extends Thread
also have its own advantages over implements Runnable
and if so, what are these advantages?
Because sometimes (almost never, but sometimes) you want to be able to change the basic behaviour of Thread
.
That's when you'll need to extend it.
You can change it by overriding a method from the Thread
class, you can't do it by implementing one from Runnable
.
start
to do so. –
Castoff In the last 20+ years since Java 1.0 was released, what is a considered a good design pattern has changed. However, Java is committed to backward compatibility which means old code which might use poor design patterns will still work.
One of my pet hates is StringBuffer
for which it was never a good idea to make it's method synchronized, was replaced more than tens years ago, but unfortunately developers are not prevented from using it today and even new developers use it, even though it was deprecated long before they started using Java.
Thread
is very rarely about overriding anything but run()
–
Tarlatan Runnable
is somehow related to a Thread
. Well it isnt. At all. Runnables are blocking, executable algorithms and threads are asynchronous, independent ones which can still run long after the calling thread has ended. Please dont write answers about topics which are alien to you, thank you very much. This question could be reformulated to "Whats better : GM crops or the last season of breaking bad?" --- does not compute. –
Collage class Thread implements Runnable
This means that by inheritance you can pass an overridden Thread any where you use a Runnable, and you can pass a Runnable in the constructor of a Thread to be it's default implementation. If some one doesn't make sense to you, it probably just means you don't understand what they are talking about. The question does make sense as it is a common question see the "Possible duplicate" –
Tarlatan Integer
yet change its entire behaviour so that it spits out a float value if i wanted to - id' run into precision problems inside of equals
but thats about it, there are "good" approximations for that –
Collage floatValue
in Integer has a float value so I don't see what you are referring to. Instead of talking about something theoretical which might make sense to you, I suggest you consider how Java is actually used. –
Tarlatan © 2022 - 2024 — McMap. All rights reserved.