The difference between runnable and thread in Java

Source: Internet
Author: User
Tags ticket

The difference between runnable and thread in Java

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.

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:

    1. package org.thread.demo;  
    2. class mythread extends thread{ 
    3. private string name;  
    4. Public mythread (string name)  { 
    5. super ();  
    6. this.name =  Name  
    7. }  
    8. Public void run () { 
    9. for (int i=0;i<10;i++) { 
    10. System.out.println ("Thread Start:" +this.name+ ", i=" +i);  
    11. }  
    12. }  
    13. }  < /li>
    14. package org.thread.demo;  
    15. public class threaddemo01 { 
    16. Public static void main (String[] args)  { 
    17. mythread mt1=new  MyThread ("Thread A");  
    18. mythread mt2=new mythread ("thread B");  
    19. Mt1.run ();  
    20. Mt2.run (); nbsp;
    21. }  

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:

    1. Package Org.thread.demo;
    2. public class ThreadDemo01 {
    3. public static void Main (string[] args) {
    4. MyThread mt1=new MyThread ("Thread A");
    5. MyThread mt2=new MyThread ("thread B");
    6. Mt1.start ();
    7. Mt2.start ();
    8. }
    9. };

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.

    1. public interface runnable{
    2. public void run ();
    3. }

Example:

    1. Package Org.runnable.demo;
    2. Class MyThread implements runnable{
    3. private String name;
    4. Public MyThread (String name) {
    5. THIS.name = name;
    6. }
    7. public void Run () {
    8. for (int i=0;i<100;i++) {
    9. SYSTEM.OUT.PRINTLN ("Thread Start:" +this.name+ ", i=" +i);
    10. }
    11. }
    12. };

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):

    1. Package Org.runnable.demo;
    2. Import Org.runnable.demo.MyThread;
    3. public class ThreadDemo01 {
    4. public static void Main (string[] args) {
    5. MyThread mt1=new MyThread ("Thread A");
    6. MyThread mt2=new MyThread ("thread B");
    7. New Thread (MT1). Start ();
    8. New Thread (MT2). Start ();
    9. }
    10. }

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:

    1. Package ORG.DEMO.DFF;
    2. Class MyThread extends thread{
    3. private int ticket=10;
    4. public void Run () {
    5. for (int i=0;i<20;i++) {
    6. if (this.ticket>0) {
    7. SYSTEM.OUT.PRINTLN ("Sell ticket: Ticket" +this.ticket--);
    8. }
    9. }
    10. }
    11. };

Below through three thread objects, while selling tickets:

    1. Package ORG.DEMO.DFF;
    2. public class Threadticket {
    3. public static void Main (string[] args) {
    4. MyThread mt1=new MyThread ();
    5. MyThread mt2=new MyThread ();
    6. MyThread mt3=new MyThread ();
    7. Mt1.start ();//each thread sold 10 sheets, sold 30 tickets.
    8. Mt2.start ();//But actually there are only 10 tickets, and each thread sells its own ticket.
    9. Mt3.start ();//failed to achieve resource sharing
    10. }
    11. }

If you can use runnable to achieve resource sharing, see the example below:

    1. Package org.demo.runnable;
    2. Class MyThread implements runnable{
    3. private int ticket=10;
    4. public void Run () {
    5. for (int i=0;i<20;i++) {
    6. if (this.ticket>0) {
    7. SYSTEM.OUT.PRINTLN ("Sell ticket: Ticket" +this.ticket--);
    8. }
    9. }
    10. }
    11. }
    12. Package org.demo.runnable;
    13. public class Runnableticket {
    14. public static void Main (string[] args) {
    15. MyThread mt=new MyThread ();
    16. New Thread (MT). Start ();//the same MT, but not in Thread, if you use the same
    17. New Thread (MT). Start ();//An instantiation of the object Mt will cause an exception
    18. New Thread (MT). Start ();
    19. }
    20. };

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.