There are two ways to implement multithreading in Java, one is to inherit the thread class, one to implement the Runnable interface, and the thread class to be defined in the Java.lang package. A class that inherits the thread class to overwrite the run () method in this class can implement multithreaded operations, but a class can inherit only one parent class, which is the limitation of this method.
Let's look at the example below:
PackageOrg.thread.demo; classMyThreadextendsthread{PrivateString name; PublicMyThread (String name) {Super(); This. Name =name; } Public voidrun () { for(inti=0;i<10;i++) {System.out.println ("Thread Start:" + This. name+ ", i=" +i); }}} PackageOrg.thread.demo; Public classThreadDemo01 { Public Static voidMain (string[] args) {MyThread mt1=NewMyThread ("Thread A"); MyThread mt2=NewMyThread ("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. Start Here
The start () method starts the thread:
Package Org.thread.demo; Public class ThreadDemo01 { publicstaticvoid 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 becomes a 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.
public interface runnable{
public void run ();
}
Example:
package Org.runnable.demo; class MyThread implements runnable{ private String name; public MyThread (String name) { Span style= "color: #0000ff;" >this . Name = name; public void run () { for (int i=0;i<100;i+ + thread start: "+this . Name+ ", i=" +i); }}};
However, there is no start () method in a subclass defined with runnable, only in the thread class. At this point the thread class is observed, and there is a constructor method: Public Thread (Runnable Targer) This construction method accepts the subclass instance of Runnable, which means that the thread class can be used to start the multithreading of the Runnable implementation. (Start () to coordinate the resources of the system):
package Org.runnable.demo; import Org.runnable.demo.MyThread; public class ThreadDemo01 { public static Span style= "color: #0000ff;" >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:
Package ORG.DEMO.DFF; classextends thread{ privateint ticket=10; publicvoid run () { for (int i=0;i<20;i++) { if (this. ticket>0) {System.out.println ("Sell ticket: Ticket" +this. ticket--); }}};
Below through three thread objects, while selling tickets:
PackageORG.DEMO.DFF; Public classThreadticket { Public Static voidMain (string[] args) {MyThread mt1=NewMyThread (); MyThread mt2=NewMyThread (); MyThread Mt3=NewMyThread (); Mt1.start ();//each thread sold a total of 10, and sold 30 tickets .Mt2.start ();//but actually there are only 10 tickets, and each thread sells its own ticket.Mt3.start ();//resource sharing not achieved}}
If you can use runnable to achieve resource sharing, see the example below:
Packageorg.demo.runnable; classMyThreadImplementsrunnable{Private intticket=10; Public voidrun () { for(inti=0;i<20;i++) {if( This. ticket>0) {System.out.println ("Sell ticket: Ticket" + This. ticket--); } }}} Packageorg.demo.runnable; Public classRunnableticket { Public Static voidMain (string[] args) {MyThread Mt=NewMyThread (); NewThread (MT). Start ();//The same MT, but not in thread, if you are using the same NewThread (MT). Start ();//an instantiated object MT, an exception occurs NewThread (MT). Start (); }};
Although the program now has three 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
found that the thread class is also a subclass of the Runnable interface.
"Go" Java Thread family---the difference between runnable and thread