Difference analysis of runnable and thread in Java _java programming

Source: Internet
Author: User

The thread class is defined in the Java.lang package. A class can implement multithreading as long as it inherits the thread class while it is overridden by the run () method in this class, but a class can inherit only one parent class, which is the limitation of this method.

Here's a look at the example:

Copy Code code as follows:

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. Start below

The start () method starts the thread:

Copy Code code as follows:

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 technology becomes 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.

Copy Code code as follows:

public interface runnable{

public void run ();

}


Example:
Copy Code code as follows:

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. The thread class is observed at this time, and there is a construction method: Public Thread (Runnable Targer) This constructor accepts the subclass instance of Runnable, that is, the thread class can be used to start the multithreading of Runnable implementations. (Start () can coordinate the system's resources):

Copy Code code as follows:

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:

Copy Code code as follows:

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 ("Selling Tickets: Ticket" +this.ticket--);

}

}

}

};


The following three thread objects, while selling tickets:
Copy Code code as follows:

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 sold 10 pieces each, sold a total of 30 tickets

Mt2.start ()//But actually there are only 10 tickets, each thread sells its own ticket

Mt3.start ()//does not meet resource share

}

}


If you can implement resource sharing with runnable, here's an example:
Copy Code code as follows:

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 ("Selling Tickets: 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, if you use the same

New Thread (MT). Start ()///The instantiated object MT, an exception is found

New Thread (MT). Start ();

}

};


Although there are now three threads in the program, but sold a total of 10 tickets, that is, using runnable to achieve the goal of sharing resources.

The connection between runnable interface and thread:

public class Thread extends Object implements Runnable

found that 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.