Two ways to create a thread compare thread VS Runnable

Source: Internet
Author: User
Tags stub thread class ticket

1. First of all, two ways to create a thread

One way is to inherit the thread class and override the run () method

1 public class MyThread extends thread{2     @Override 3 public     void Run () {4         //TODO auto-generated method stub 5          6     } 7} 8//thread using 9 MyThread mt = new MyThread ();  Create thread Mt.start ();                   Start thread

Another way is to implement the Runnable interface

1 public class MyThread implements runnable{2     @Override 3 public     void Run () {4         //TODO auto-generated Metho D stub 5          6     } 7} 8//thread using 9 MyThread mt = new MyThread (), Thread thread = new Thread (MT),//create thread one by one Thread.Start ();                   Start thread

2. Two ways to create a thread comparison

1th: Through the creation of threads can be seen, one is to inherit one is to implement the interface, but Java can only inherit a parent class, it is possible to implement a feature of multiple interfaces, so the use of runnable method can avoid the thread mode due to Java single inheritance caused by the flaw.

2nd: runnable code can be shared by multiple threads (thread instances) and is suitable for multiple threads to handle uniform resources.

Example: Simulated sell tickets, assuming that there are 5 tickets left, two ways to create a threading simulation

Ticketthread.java//The case of using inheritance to simulate 3 windows selling 5 tickets

1 public class Ticketthread {2 public     static void Main (string[] args) {3         //Create 3 threads, simulate three windows sell ticket 4         MyThread MT1 = New MyThread ("Thread One"); 5         MyThread mt2 = new MyThread ("Thread Two"), 6         MyThread mt3 = new MyThread ("Thread Three"), 7          8         //boot thread 9         mt1.start ();         Mt2.start (),         Mt3.start (),     }13}15, class MyThread extends thread{17     private int num = 5;//mode There are 5 tickets left. The     private String name;//is used to indicate the thread name,     MyThread (string name) {         this.name = name;     @Override25 public     Void Run () {}23         (num > 0) {             num--;28             System.out.println (name+ "sold a ticket, the remaining votes are" +num);         }30     }31}

The result of the operation is:

1 Thread one sold a ticket, the remaining votes are 4 2 thread one sold a ticket, the remaining votes are 3 3 thread one sold a ticket, the remaining votes of 2 4 thread one sold a ticket, the remaining votes of 1 5 thread one sold a ticket, the remaining votes of 0 6 thread two sold a ticket, the remaining votes of 4 7 thread two sold One ticket, the remaining votes are 3 8 thread two sold a ticket, the remaining votes are 2 9 thread two sold a ticket, the remaining votes of 110 thread two sold a ticket, the remaining votes of 011 thread three sold a ticket, the remaining votes of 412 thread three sold a ticket, the remaining votes of 313 thread three sold a ticket, The remaining votes are 214 threads three sold a ticket, the remaining votes of 115 thread three sold a ticket, the remaining votes of 0

You can see that 15 tickets have been sold, and 5 tickets have been turned into each thread.

Ticketrunnable.java//with runnable implementation

1 public class tickerrunnable{2 public     static void Main (string[] args) {3         MyThread mt = new MyThread (); 4         // Also create 3 threads of 5 thread         t1 = new Thread (MT, "thread One"), 6         thread t2 = new Thread (MT, "thread Two"), 7         thread t3 = new Thread (MT , "thread Three"); 8         //Start thread 9         t1.start ();         T2.start ();         T3.start ();     }13}15 class MyThread implements RUNNABLE{17     private int num = 5;//simulation also has 5 tickets remaining     @Override20 public     void Run () {         > 0) {             num--;23             System.out.println (Thread.CurrentThread () getName () + "sold a ticket with the remaining votes" +num);     }26}

The result of the operation is:

1 Thread one sold a ticket, the remaining votes of 42 thread three sold a ticket, the remaining votes of 33 thread three sold a ticket, the remaining votes of 14 thread two sold a ticket, the remaining votes of 05 thread one sold a ticket, the remaining votes of 2

This shows that multiple threads share the 5 variable in the Runnbale, as to why the result is not the output of the remaining votes 4 3 2 1 0, this is due to thread scheduling problems, each run will see a different result.

Two ways to create a thread compare thread VS Runnable

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.