Differences between multi-thread runable and thread

Source: Internet
Author: User
Two implementation methods of Multithreading
In Java, You can implement multithreading in two ways. 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. A class inherits the Thread class and overwrites

The run () method can implement multi-threaded operations, but a class can only inherit one parent class, which is a limitation of this method,

The following is 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 ");
Mythread MT2 = new mythread ("thread B ");
Mt1.run ();
Mt2.run ();
}
}
However, the results are regular at this time. The first object is executed first, and then the second object is executed without running each other. In

JDK documentation shows that once the START () method is called, The run () method is found through JVM. Start

Start () method start thread:
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 normally complete interactive operation. So why do we have to use the START (); Method to start multithreading?
In the installation environment of JDK, src.zip is the full Java source program. Through this code, find the START () side in the thread.

Private native void start0 () is used in this method, where the native keyword table

Shows that the underlying functions 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 (){
For (INT I = 0; I <100; I ++ ){
System. Out. println ("thread start:" + this. Name + ", I =" + I );
}
}
};
However, the start () method is not available in the subclass defined by runnable, and is available only in the Thread class. Observe

Thread class, which has a constructor: Public thread (runnable targer)
This constructor accepts runnable subclass instances, that is, you can use the Thread class to start multiple runnable implementations.

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

Inheritance of the thread class has the following advantages:-> to 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, the use of runnable to implement Multithreading

To share resources.

· The connection between the runnable interface and thread:
Public class thread extends object implements runnable
The thread class is also a subclass of the runnable interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.