I know the differences between the concrete Thread class and the Runnable Interface in Java . What is the need for making the Thread class overridable so that it is available for consumption ? Can all threads be created by implementing the Runnable interface ? What will be the use case where the Thread class is a necessity? Why we have two ways to solve the same problem ?
CodePudding user response:
Why Java provides both the Thread Class and Runnable Interface for creating Threads?
The short answer is "history".
In ~1995, Java 1.0 defined Thread
so that you can either extend the class, or pass a Runnable
to the constructor. By about Java 1.1, it was pretty clear to the designers that extending Thread
was a poor choice. But it was too late to fix. Changing Thread
to be a final
class was no longer an option, since it would break backwards compatibility.
Roll forward to 2022 and the same reasoning still applies. There is still lots of important legacy code where programmers have extended Thread
to override run()
with a method containing application logic. The code works fine ... so forcing lots of Java users to update to a "better" way would be a bad business decision.
Why did they make the decision in the first place? Who knows! My guess is that back in 1995 they didn't envisage things like thread pools, executors and so on that are problematic if you extend Thread
. (And a few other things ... as illustrated by the unsafe or unimplementable Thread
methods that were deprecated many years ago.)
One possible factor is that Java 1.0 didn't have anonymous inner classes. So, to use the constructor parameter approach, programmers needed to declare a named class that extended Runnable
. Extending Thread
was an "attractive" alternative. IIRC, that language shortcoming was addressed in Java 1.1. And in Java 8 we can also use lambda expressions to define thread runnables.
Remember: threads were a new thing when Java was designed. Java was one of the first programming languages to support them properly. (The only way to guarantee not to make mistakes is to not do anything. And that is often a bigger mistake.)
Can all threads be created by implementing the
Runnable
interface ?
No. All of the logic involved in creating a thread is actually in the Thread
class ... and its native code implementation in the JVM. It is not practical to do this yourself in Java code.
CodePudding user response:
Java doesn't have multiple inheritance concept for classes ,but for interface it is .
Let's say you have a class which is already extending(inheriting) a class
class A extends Parent{
}
Above class can't extend another class like
class A extends Parent extends Thread{ // Incorrect
}
But it can have something like
class A extends Parent implements Runnable{
}
Now for above case if you want to utillize multi threading
,you can easily do that.
For more look here
CodePudding user response:
The Thread is the object that connects the java code to the OS thread, it provides the means to run code on that thread. The Runnable is a task that can be executed on a Thread.
There is no good reason I have ever found to extend Thread, except maybe it is convenient in a demo to have only one class. When Java was new I suspect easy looking demos were a priority.
A Thread's properties like ID, name, priority, etc, should be set before it starts, you can use a ThreadFactory to create threads with the properties you want. Use Runnable (or better, Callable) for the tasks you need executed. The Runnable doesn't need to know how it is being executed, it just needs to implement the desired logic.
CodePudding user response:
Because if we extend Thread class the we can't extend any other class so that reason java provide runnable interface it is higly recomanded to use runnable interface if we implement runnable interface then we can extends any other also