The difference between runnable and thread in Java

Source: Internet
Author: User

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 only one parent class can inherit from a class, which is the limitation of this method.

Let's look at the example below:

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 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. Starting the Start () method starts the thread:

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 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) {
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 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 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 development of the program as long as the multithreading must always be implemented runnable interface mainly, because the implementation of the Runnable interface compared to inherit the thread class has the following advantages:

To avoid the limitations of point inheritance, a class can inherit multiple interfaces.
Resource-friendly sharing
Take the ticket selling procedure as an example, complete by the thread class:

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 ("Sell ticket: Ticket" +this.ticket--);
}
}
}
};
Below through three thread objects, while selling tickets:

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 sheets, sold 30 tickets.
Mt2.start ();//But actually there are only 10 tickets, and each thread sells its own ticket.
Mt3.start ();//failed to achieve resource sharing
}
}
If you can use runnable to achieve resource sharing, see the example below:

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 ("Sell 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 ();//the same MT, but not in Thread, if you use the same
New Thread (MT). Start ();//An instantiation of the object Mt will cause an exception
New Thread (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.

The difference between runnable and thread in Java

Related Article

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.