Java Multiple Threads Thread class inheritance difference and implement Runnable

Java threads created in two ways:

    1. Thread class by inheritance, the rewriting Thread run () method, the thread running in a logical wherein

    2. By implementing Runnable interface, the Thread class instantiation

    In practical applications, we often use multi-threading, such as the station's ticketing system, the station ticket window is equivalent to each individual threads. When we do this system, we might think of two ways to achieve, inheritance Thread class or implement Runnable interface, and now look at the two results of these two methods to achieve.

A: inheritance Thread class

 1 package com.threadtest;  
 2 class MyThread extends Thread{  
 3       
 4     private int ticket = 10;  
 5     private String name;  
 6     public MyThread(String name){  
 7         this.name =name;  
 8     }  
 9       
10     public void run(){  
11         for(int i =0;i<500;i++){  
12             if(this.ticket>0){  
13                 System.out.println ( the this .name + "ticket ---->" + ( the this .ticket-- ));  
 14              }  
 15          }  
 16      }  
 . 17  }  
 18 is  public  class ThreadDemo {  
 . 19    
20 is        
21 is      public  static  void main (String [] args) {  
 22 is          the MyThread MTl = new new the MyThread ( "One window" );  
 23 is          the MyThread MT2 = new new the MyThread ( "window II" );  
 24          the MyThread MT3 = new new the MyThread ( "window 3");  
25         mt1.start();  
26         mt2.start();  
27         mt3.start();  
28     }  
29   
30 }  

 

Results are as follows: multiple windows to complete multiple tasks

 

Two: the way you implement Runnable

package com.threadtest;  
class MyThread1 implements Runnable{  
    private int ticket =10;  
    private String name;  
    public void run(){  
        for(int i =0;i<500;i++){  
            if(this.ticket>0){  
                System.out.println(Thread.currentThread().getName()+"卖票---->"+(this.ticket--));  
            }  
        }  
    }  
}  
public class RunnableDemo {  
  
      
    public  static  void main (String [] args) {  
         // the TODO Auto-Generated Method Stub  
         // design three threads   
         MyThread1 MT = new new MyThread1 ();  
         T1 the Thread = new new the Thread (MT, "One Window" );  
         T2 the Thread = new new the Thread (MT, "Window II" );  
         Thread t3 = new Thread(mt,"三号窗口");  
//         MyThread1 mt2 = new MyThread1();  
//         MyThread1 mt3 = new MyThread1();  
         t1.start();  
         t2.start();  
         t3.start();  
    }  
  
}  

 

Results are as follows: multiple windows to complete a task

Three: a comparison between the two

    Why two different results appear na? We might as well be a metaphor, in fact, on top of the program,

    Inherit the Thread class, we come up with three things that is equivalent to selling 10 tickets three tasks were given to three windows, each doing what they each sell 10 tickets each to complete their respective tasks, because MyThread thread class inheritance, so when the new MyThread, and in the creation of three objects at the same time creates three threads;

    Implement Runnable, it is to come up with the equivalent of a task to sell 10 tickets together to complete the three windows, new MyThread equivalent to creating a task, and then instantiate three Thread, create three threads that is scheduled to perform three windows .

   When we are new may be confused inherit the Thread class and Runnable interface to achieve multi-threaded, in fact, after the contact we will find it is an entirely different multi-threading, multiple threads each one is to complete their task, a multiple threads together to complete a task.

    In fact, to achieve a task to do with multiple threads can also be used to implement the Thread class inherits just too much trouble, we usually use to achieve Runnable interface to achieve, clear and concise.

    In most cases, if you want to override the run () method, without overwriting other Thread methods, you should use the Runnable interface. This is important, because unless the programmer intends on modifying or enhancing the fundamental behavior of the class, it should not create a sub-class this (Thread).

 

Reprinted: https: //www.cnblogs.com/zhaosq/p/9882102.html

Guess you like

Origin www.cnblogs.com/sunbr/p/11878215.html