A Brief analysis of the differences between Runnable and Thread in Java

  • 2020-06-07 04:27:36
  • OfStack

Instead of simply calling your RUN method, start with a thread scheduler that calls each of your RUN methods,
We ordinary RUN method if not performed is not returned, can be carried 1 straight down, so RUN methods the following methods could not be performed, but doesn't thread RUN method 1 sample, it is only 1 set CPU time, after the execution to other threads, so that repeatedly cut CPU time to cut off, because the switching speed quickly, so we feel are many threads run 1 sample at the same time.

Simply calling the run method doesn't have the same effect, so you have to call the start method of the Thread class to start your thread

1 is to write a class that inherits from the Thread class, and then override the run method inside to start the thread with the start method

2 is to write 1 class to implement Runnable interface, implement the run method inside, use new Thread(Runnable target).start () method to start

Both methods must implement the RUN method so that the thread manager can call your RUN method when the thread starts.

If your TestThread does not inherit from the Thread class, how can you have an start method?

There are two ways to implement multithreading in java, one is to inherit Thread class, the other is to implement Runnable interface. The Thread class is defined in the ES45en.lang package. A class that inherits the Thread class and overrides the run() method in this class can achieve multithreading, but a class can only inherit from one parent, which is a limitation of this method.

Here's an example:


package org.thread.demo; 
class MyThread extends Thread{ 
private String name; 
public MyThread(String name) { 
super(); 
this.name = name; 
} 
public void run(){ 
for(int i=0;i<10;i++){ 
System.out.println(" Thread start: "+this.name+",i="+i); 
} 
} 
} 
package org.thread.demo; 
public class ThreadDemo01 { 
public static void main(String[] args) { 
MyThread mt1=new MyThread(" thread a"); 
MyThread mt2=new MyThread(" thread b"); 
mt1.run(); 
mt2.run(); 
} 
}

However, at this point the result is very regular, the first object executes, then the second object executes, and does not run with each other. As you can see in the documentation for JDK, once the start() method is called, the run() method is found through JVM. Now start the start() method to start the thread:


package org.thread.demo; 
public class ThreadDemo01 { 
public static void main(String[] args) { 
MyThread mt1=new MyThread(" thread a"); 
MyThread mt2=new MyThread(" thread b"); 
mt1.start(); 
mt2.start(); 
} 
};

This program can normally complete the interactive operation. So why use start(); Method to start multithreading?

Under the installation path of JDK, ES67en.zip is all the java source programs. Find the definition of start() in Thread through this code. It can be found that private native void start0() is used in this method. The native keyword indicates that the underlying function of the operating system can be called, so this technique is called JNI technique (java Native Interface).

Runnable interface

In real development, one-threaded operations rarely use the Thread class, but are done through the Runnable interface.


public interface Runnable{ 
public void run(); 
}

Example:


package org.runnable.demo; 
class MyThread implements Runnable{ 
private String name; 
public MyThread(String name) { 
this.name = name; 
}
public void run(){ 
for(int i=0;i<100;i++){ 
System.out.println(" Thread start: "+this.name+",i="+i); 
} 
} 
};

But there are no start() methods in the subclasses defined using Runnable, only in the Thread class. At this point, if you look at the Thread class, you have one constructor: public Thread(Runnable targer). This constructor accepts an instance of a subclass of Runnable, which means you can use the Thread class to start the multithreading implemented by Runnable. (start() can coordinate system resources) :


package org.runnable.demo; 
import org.runnable.demo.MyThread; 
public class ThreadDemo01 { 
public static void main(String[] args) { 
MyThread mt1=new MyThread(" thread a"); 
MyThread mt2=new MyThread(" thread b"); 
new Thread(mt1).start(); 
new Thread(mt2).start(); 
} 
}

Differences and connections between the two implementations:

In program development, as long as the multithreading must always focus on implementing Runnable interface, because implementing Runnable interface has the following advantages over inheriting Thread class:

To avoid the limitation of point inheritance, a class can inherit multiple interfaces. Suitable for the sharing of resources

Taking the ticket selling program as an example, Thread class completes:


package org.demo.dff; 
class MyThread extends Thread{ 
private int ticket=10; 
public void run(){ 
for(int i=0;i<20;i++){ 
if(this.ticket>0){ 
System.out.println(" Tickets: ticket"+this.ticket--); 
} 
} 
} 
};

Here are three thread objects that sell tickets at the same time:


package org.demo.dff; 
public class ThreadTicket { 
public static void main(String[] args) { 
MyThread mt1=new MyThread(); 
MyThread mt2=new MyThread(); 
MyThread mt3=new MyThread(); 
mt1.start();// Each thread is sold 10 Zhang, sold together 30 ticket  
mt2.start();// But the reality is that 10 Each thread sells its own ticket  
mt3.start();// Resource sharing is not achieved  
} 
}

If you use Runnable, you can realize resource sharing. Here is an example:


package org.demo.runnable; 
class MyThread implements Runnable{ 
private int ticket=10; 
public void run(){ 
for(int i=0;i<20;i++){ 
if(this.ticket>0){ 
System.out.println(" Tickets: ticket"+this.ticket--); 
} 
} 
} 
} 
package org.demo.runnable; 
public class RunnableTicket { 
public static void main(String[] args) { 
MyThread mt=new MyThread(); 
new Thread(mt).start();// with 1 a mt , but in Thread Chinese is not allowed 1 
new Thread(mt).start();// Two instantiated objects mt , there will be an exception  
new Thread(mt).start(); 
} 
}; 

Although there are now three threads in the program, 1 has sold 10 tickets, which means that using Runnable to achieve multithreading can achieve the purpose of resource sharing.

Connections between the Runnable interface and Thread:


public class Thread extends Object implements Runnable

It turns out that the Thread class is also a subclass of the Runnable interface.

As you can see, implementing the Runnable interface has the following significant benefits over inheriting the Thread class:

(1) Suitable for multiple threads of the same program code to deal with the same 1 resource, virtual CPU (thread) and program code, data effective separation, better reflects the object-oriented design idea.

(2) The limitations caused by the single inheritance feature of Java can be avoided. We often encounter the situation that when we want to inherit a subclass of a certain class into multithreading, since a class cannot have two superclasses at the same time, we cannot use the way of inheriting Thread class, so the class has to implement the INTERFACE of Runnable.

(3) Conducive to the robustness of the program, the code can be Shared by multiple threads, code and data is independent. When multiple threads execute code from instances of the same class, they are said to share the same code. Multiple threads operate on the same data regardless of their code. When a share accesses the same object, they share the same data. When a thread is constructed, the required code and data are passed in as constructor arguments through an object that is an instance of a class that implements the Runnable interface.


Related articles: