What’s the difference between Thread and Thread (Runnable)?
Answer 1, authority 100%
@Futurama , in documentation the answer to your question is clearly spelled out.
Read (bold – this is my emphasis):
There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread . This subclass should override the run method of class Thread . An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows:
class PrimeThread extends Thread {
long minPrime;
PrimeThread (long minPrime) {
this.minPrime = minPrime;
}
public void run () {
// compute primes larger than minPrime
... ... ...
}
}
The following code would then create a thread and start it running:
PrimeThread p = new PrimeThread (143);
p.start ();
The bottom line is that we override the run method of the Thread class, which is called from the start method of the Thread class, which we call to start the thread (execute our program in a new thread).
The other way to create a thread is to declare a class that implements the Runnable interface . That class then implements the run method . An instance of the class can then be allocated, passed as an argument when creating Thread, and started. The same example in this other style looks like the following:
class PrimeRun implements Runnable {
long minPrime;
PrimeRun (long minPrime) {
this.minPrime = minPrime;
}
public void run () {
// compute primes larger than minPrime
... ... ...
}
}
The following code would then create a thread and start it running:
PrimeRun p = new PrimeRun (143);
new Thread (p) .start ();
In principle, as they say – “the same eggs, side view”.
If you think about it, the essence is the same. A new thread is created (prepared to run) (the Thread class is a data structure inside the JVM). This structure stores the address of a function (a sequence of JVM instructions) that represents the program that we want to execute on a new thread. Calling the .start method of the Thread class (directly or indirectly, as inherited in the first case) will start a new thread (how exactly depends on the OS in which a particular JVM is running).
Answer 2, authority 92%
- Multithreading in JAVA is by no means limited to the
Thread
class - In the context of a specific task, it may be more beneficial to inherit from some other class, but multiple inheritance in JAVA is not supported, output:
implements Runnable
- The
Runnable
interface is mediocre to threads – it should be regarded as a passable function that can be executed elsewhere (thread, queue, class, method, etc.)
Answer 3, authority 98%
Thread
is an abstraction over a physical thread.
Runnable
is an abstraction of the task being performed.
The advantage of using Runnable
is that it allows you to logically separate task execution from the flow control logic.
Answer 4, authority 57%
Runnable
is an interface describing the Run method, with which you can pass your code to another class for execution. And it has nothing to do with Thread
in the sense that it has no hidden functionality. To execute some code on a thread, the Thread
class simply uses this interface as an abstraction layer. Nothing prevents you from using Runnable
in any other way. For example, to transfer a callback.
Answer 5, authority 21%
In my opinion, the point is that multiple inheritance in Java is possible only from the interface, and if you need to implement your class from some parent, then use the Runnable interface. If you like and enough inheritance from a class, use Thread.
Answer 6, authority 21%
The Thread class has several methods that you can override in the child class. Of these, only the run () method is required to be overridden. The same method should certainly be defined when implementing the interface Runnable . Some Java programmers feel that subclassing the Thread class should only be done if new functionality needs to be added to it. So, if you do not need to override any other methods from the Thread class, then you can limit yourself only to the implementation of the Runnable interface. In addition, implementing the Runnable interface allows the generated thread to inherit a class other than Thread .