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 via the Runnable interface? This type of stream in the stream or some extra. functions will be? For example, access to the UI? ..

    6 answers 6

    @Futurama , the answer to your question is clearly stated in the documentation .

    We read (bold - this is highlighted by me):

    There are two ways to create a 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, it 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 be to 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 class Thread, which is called from the start method of the class Thread, which we call to start the thread (executing our program in a new thread).

    Declare a class that implements the Runnable interface . That class then implements the run method . An example of this can be defined as a thread, and started. It 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 be to 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 for launch) (Thread class is a data structure inside the JVM). This structure contains the address of the function (JVM instruction sequence), which represents the program that we want to run in the new thread. Calling the .start method of the class Thread (directly or indirectly, as inherited in the first case) will launch a new thread (exactly how it depends on the OS in which the particular JVM works).

    That's all. Just RTFM.

      1. Multithreading in JAVA is not limited to the class Thread
      2. In the context of a specific task, it may be more profitable to inherit some other class, but multiple inheritance in JAVA is not supported, exit: implements Runnable
      3. The Runnable interface has a mediocre relationship to threads - it should be regarded as a passed-in function that can be executed somewhere else (stream, queue, class, method, etc.)

        Thread is an abstraction over physical flow.

        Runnable is an abstraction of the task being performed.

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

        • Runnable is not bullshit, but an interface. And an interface for inheritance. It looks like you never had to deal with diamond-shaped inheritance. - arg
        • one
          @Futurama well, about the trade you understand, it means the language of man. Runnable is not a garbage, but a task that can be performed in the stream (in the current or specially created for this purpose). Obviously, a single thread can perform multiple tasks one after another. And if so, then we need to be able to separate one from the other so that it can be managed. Examples in the open spaces of the network can dig up a lot. - a_gura
        • @Futurama, well, give an example of how to run a task in Thread without using Runnable . - Helisia
        • Judge for yourself @Futurama, if we know that we have a thread and a task is already associated with it, then it would be convenient to have a way to start the task in this thread. Well, of course, a thread cannot perform tasks without Runnable due to the implementation (Thread implements Runnable) :) - a_gura
        • @Futurama do you understand what this code does? I answer: implements the Runnable interface :) - a_gura

        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 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 any other way. For example, to transfer kollbeka.

        Thread is a class that implements a thread and uses the Runnable interface to embed your code into a stream.

          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 the class, use Thread.

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