In Java, there are two ways to implement multithreading, one is to inherit the thread class, one is to implement the Runnable interface;
The thread class is defined in the Java.lang package. A class simply inherits the thread class and then repeats the
The run () method enables multithreading, but a class can inherit only one parent class, which is the limitation of this method,
Here's a look at the 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, the results are very regular, first the first object executes, then the second object executes, and does not run with each other. As you can see in the JDK documentation, once the start () method is invoked, the run () method is found through the JVM. The start Start () method starts the thread below:
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 do you have to use the start () method to start multithreading.
Under the JDK installation path, Src.zip is the entire Java source program, which finds the definition of the start () method in thread, and discovers that the private native void Start0 () is used in this method; Where the Native keyword indicates that the underlying function of the operating system can be invoked, such a technique is called JNI technology (Java Native Interface)
· Runnable interface
In actual development, a multithreaded operation rarely uses the thread class, but is done through the Runnable interface.
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);
}
}
However, there is no start () method in subclasses that are defined using runnable, only in the thread class. To observe the Thread class at this point, there is a construction method: Public Thread (Runnable Targer)
This constructor accepts a subclass instance of runnable, which means that the thread class can be used to start the multithreading of the runnable implementation. (Start () can coordinate the system's 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 ();
}
}
The difference and connection between the two ways of implementation:
In the program development as long as multithreading is definitely always to achieve runnable interface, because the implementation of runnable interface compared
Inheriting the thread class has the following benefits:
-> avoid the limitations of point inheritance, a class can inherit multiple interfaces.
-> is suitable for resource sharing
Take the ticket-selling procedure as an example, through the thread class to complete:
Thread implementation threads cannot implement resource sharing class Mythread extends thread {private int ticket=10;
private String name;
Public Mythread (String name) {this.name=name;
public void Run () {() {if (ticket>0) {i=0;i<10;i++) {System.out.println ("thread" +name+ "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 implementation thread to implement resource sharing class Mythread implements Runnable {private int ticket=10;
private String name;
Public Mythread (String name) {this.name=name;
public void Run () {() {if (ticket>0) {i=1;i<=10;i++) {System.out.println ("thread" +name+ "Ticket" + (ticket--)); }}} public class Threadticket {public static void main (String args[]) {Mythread a = new Mythread ("a"); The task that the instantiated thread is to perform thread Ta = new Thread (A); Instance two thread objects that actually pass a task thread Tb = new Thread (A); Because two threads perform 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 to achieve the goal of sharing resources.
Connection between Runnable interface and thread: public class Thread extends Object implements Runnable
It is easy to see that the thread class is a subclass of the Runnable interface.