JAVA supports two methods to implement multithreading. One is to inherit the Thread class and the other is to implement the runnable interface;
the Thread class is in Java. defined in the lang package. A class can implement multi-threaded operations as long as it inherits the Thread class and overwrites the
Run () method in this class, however, a class can only inherit one parent class, which is a limitation of this method.
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 ");
mythread MT2 = new mythread ("thread B");
mt1.run ();
mt2.run ();
}
however, the results are regular at this time. First, the first object is executed, and then the second object is executed. In the JDK documentation, we can find that once the START () method is called, The run () method is found through JVM. Start below
Start () method:
package Org. thread. demo;
public class threaddemo01 {
Public static void main (string [] ARGs) {
mythread MT1 = new mythread ("thread ");
mythread MT2 = new mythread ("thread B");
mt1.start ();
mt2.start ();
}
}; in this way, the Program can To complete the interactive operation normally. So why do we have to use the START (); Method to start multithreading?
after JDK is installed, src.zip is a full Java source program. In this Code , locate start () in thread () method definition. You can find 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 technology becomes the JNI technology (Java Native Interface)
· runnable interface
in actual development, a multi-threaded operation rarely uses the Thread class, but is completed 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 () {
Fo R (INT I = 0; I <100; I ++) {
system. out. println ("thread start:" + this. name + ", I =" + I );
}
};
but the start () method is not available in the subclass defined by runnable, and is available only in the Thread class. Observe the Thread class. There is a constructor: Public thread (runnable targer)
This constructor accepts the subclass instance of runnable, that is to say, multiple runnable implementations can be started through the Thread class
Thread. (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 ");
Mythread MT2 = new mythread ("thread B ");
New thread (MT1). Start ();
New thread (MT2). Start ();
}
}
· Differences and links between the two implementation methods:
In program development, as long as it is multi-thread, it will always be dominated by the implementation of the runnable interface, because the implementation of the runnable interface is
Inheriting the Thread class has the following benefits:
-> Avoid the limitations of point inheritance. A class can inherit multiple interfaces.
-> Suitable for Resource Sharing
Taking the ticket selling program as an example, the thread class is used to complete:
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 ("Ticket: Ticket" + this. Ticket --);
}
}
}
};
The following uses three thread objects to 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 sells 10 tickets, with a total of 30 tickets.
Mt2.start (); // but there are only 10 tickets, each thread sells its own tickets
Mt3.start (); // resource sharing failed
}
}
If runnable is used, resources can be shared. The following 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 ("Ticket: Ticket" + this. Ticket --);
}
}
}
}
Package org. Demo. runnable;
Public class runnableticket {
Public static void main (string [] ARGs ){
Mythread Mt = new mythread ();
New thread (MT). Start (); // same Mt, but not in thread.
New thread (MT). Start (); // an exception occurs when the object MT is instantiated.
New thread (MT). Start ();
}
};
Although there are currently three threads in the program, a total of 10 tickets are sold. That is to say, using runnable to implement multithreading can achieve resource sharing.
connection between the runnable interface and thread:
public class thread extends object implements runnable
it is found that the Thread class is also a subclass of the runnable interface.