What is the difference between Thread and Runnable?


What is the difference between Thread and Thread (Runnable)?

In other words, what is the advantage of the fact that the Thread thread will be implemented through the Runnable interface? Is it like a thread in a thread or will there be some additional functions? For example, access to the UI?..

Author: Виталина, 2015-02-16

6 answers

@Futurama, the documentation clearly states the answer to your question.

Read (bold - this is highlighted by me):

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 (execution of 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 being created (prepared for launch) (the Thread class is a data structure inside the JVM). To this structure the address of the function (a sequence of JVM instructions) that represents the program we want to execute in the new thread is entered. 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 the particular JVM is running).

That's actually all. Just RTFM.

 14
Author: avp, 2015-02-17 10:57:14
  1. Multithreading in JAVA is not limited to the Thread {[5 class]}
  2. In the context of a specific task, it may be more profitable to inherit some other class, but multiple inheritance is not supported in JAVA, output: implements Runnable
  3. The interface Runnable has a mediocre relationship to threads - it should be regarded as a passed function that can be executed somewhere else (thread, queue, class, method, etc.)
 19
Author: woesss, 2015-02-16 20:39:38

Thread - this is an abstraction over the physical flow.

Runnable - this is an abstraction over the task being performed.

The advantage of using Runnable is that it allows you to logically separate the task execution from the flow control logic.

 16
Author: a_gura, 2016-12-19 08:58:36

Runnable - this is an interface that describes the Run method, which you can use to pass your code to another class for execution. And it is not related to Thread in any way, in the sense that it does not carry any hidden functionality. To execute some code in 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 send a callback.

Thread - this is a class that implements a stream and uses interface Runnable for embedding your code in a stream.

 8
Author: Чад, 2015-02-17 07:25:19

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 have enough inheritance from the class, use Thread .

 3
Author: Алексей П, 2016-02-09 07:44:36

In the classroom Thread there are several methods that can be overridden in the generated class. Of these, only the run() method is subject to mandatory redefinition. The same method should certainly be defined when implementing the interface Runnable. Some Java programmers believe that creating a subclass generated from the Thread class should only be done if you need to add new functions to it. So, if you do not need to override any other methods from the Thread class, then you can only implement the Runnable interface. In addition, the implementation of the Runnable interface allows the created thread to inherit a class other than Thread.

 3
Author: Sergey, 2017-01-11 12:40:01