In Java, there are two ways to implement multi-threading, one is to inherit the thread class, and the other is to implement the Runnable interface;
The thread class is defined in the Java.lang package. As long as a class inherits the thread class, it also overwrite the
The run () method enables multithreaded operations, but a class can inherit only one parent class, which is the limit of this method.
Let's look at the example below:
[Java] view plaincopy
-
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<; 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, the result is very regular, the first object executes, and then the second object executes, and it does not run against one another. As you can see in the JDK documentation, once the start () method is called, the Run () method is found through the JVM. Starting the Start () method starts the thread:
[Java] view plaincopy
-
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 allows the program to complete the interactive operation normally. So why not use Start ();
Under the JDK installation path, Src.zip is the entire Java source program, and this code finds the definition of the start () method in thread, and you can see that the private native void Start0 () is used in this method; Where the Native keyword means that the underlying function of the operating system can be called, then such a technique is called JNI technology (Java Native Interface)
· Runnable interface
In real-world development, a multithreaded operation rarely uses the thread class, but is accomplished through the Runnable interface.
[Java] view plaincopy
-
package org.runnable.demo;
-
class MYTHREAD&NBSP implements runnable{
-
private string name;
-
public mythread (string name) {
-
this . name = name;
-
}
-
public , void run () {
-
for ( int i= 0 ; i< + ; i++) {
-
System.out.println ( thread Start: + this . name+ ", i=" +i);
-
}
-
}
-
}
However, there is no start () method in a subclass defined with runnable, only in the thread class. There is a constructor method for observing the thread class at this point: public Thread (Runnable Targer)
This construction method takes a subclass instance of runnable, which means that the thread class can be used to start multithreading for runnable implementations. (Start () to coordinate the resources of the system):
[Java] view plaincopy
-
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 ();
-
}
-
}
The difference between the two ways of implementation and contact:
In the program development, as long as the multithreading is always to achieve runnable interface-based, because the implementation of runnable interface compared to
Inheriting the thread class has the following benefits:
To avoid the limitations of point inheritance, a class can inherit multiple interfaces.
--Suitable for resource sharing
Take the ticket selling procedure as an example, complete by the thread class:
[Java] view plaincopy
-
//Use thread to implement threads that cannot implement resource sharing
-
class MyThread extends Thread
-
{
-
Private int ticket=ten;
-
Private String name;
-
Public MyThread (String name) {
-
this. name=name;
-
}
-
Public void Run () {
-
for (int i=0;i<; i++) {
-
if (ticket>0) {
-
System.out.println ("thread"+name+"Sell ticket"+ (ticket--));
-
}
-
}
-
-
}
-
}
-
-
Public class Threadticket
-
{
-
Public Static void Main (String args[]) {
-
MyThread a = new MyThread ("a");
-
MyThread B = new MyThread ("B");
-
A.start ();
-
B.start ();
-
}
-
}
-
-
//Use runnable to implement threads for resource sharing
-
class MyThread implements Runnable
-
{
-
Private int ticket=ten;
-
Private String name;
-
Public MyThread (String name) {
-
this. name=name;
-
}
-
Public void Run () {
-
for (int i=1; i<=; i++) {
-
if (ticket>0) {
-
System.out.println ("thread"+name+"Sell ticket"+ (ticket--));
-
}
-
}
-
}
-
}
-
Public class Threadticket
-
{
-
Public Static void Main (String args[]) {
-
MyThread a = new MyThread ("a"); //Instantiate a thread to perform the task
-
Thread Ta = new thread (A); //Instance two thread object, actually passing a task
-
Thread Tb = new thread (A); //Because two threads are performing a task, resources are shared
-
Ta.start ();
-
Tb.start ();
-
}
-
}
Although the program now has two threads, but sold a total of 10 tickets, that is, the use of runnable implementation of multi-threading can achieve the purpose of resource sharing.
runnable The connection between the interface and the thread: Public class Thread extends Object implements Runnable
It is easy to see that the thread class is a subclass of the Runnable interface.
Java multithreaded--thread class and Runable interface