Java Multithreading Implementation

Source: Internet
Author: User
Tags thread class ticket

There are two ways to implement multithreading in Java, one is to inherit the thread class, and the other is to implement the Runnable interface.

So what is the difference between inheriting the thread class and implementing the Runnable interface? Concrete through the actual case to reflect.

Now simulate train ticketing system, to achieve through four sales sites to sell a certain day train 100 tickets, a site with a thread representation.

The first implementation is using the inherited thread class:

 Public classThreadTestextendsthread{Private intTicket = 100;  Public voidrun () { while(true){            if(Ticket > 0) {System.out.println (Thread.CurrentThread (). GetName ()+ "is saling ticket" + ticket--); }Else{                 Break; }        }    }}

The corresponding test class:

 Public class Testdemo {    publicstaticvoid  main (string[] args) {        new  threadtest ();         New threadtest ();                 New threadtest ();         New threadtest ();                    T1.start ();        T2.start ();        T3.start ();        T4.start ();                    }

The results of the operation are as follows:

 hread-0is saling ticket100thread -0is saling Ticket99thread -0is saling ticket98thread - 0is saling ticket97thread -0is saling Ticket96thread -0is saling ticket95thread - 1is saling ticket100thread -1is saling Ticket99thread -1is saling ticket98thread - 1is saling ticket97thread -1is saling ticket96 ... 

The results show that each ticket is printed four times, that is, four threads each sell their 100 tickets, instead of selling the same 100 tickets. How is this situation caused? What we need is that multiple threads deal with the same resource, a resource can only correspond to one object, in the above program, we create four threadtest objects, it is equal to create four resources, each resource has 100 tickets, each thread is working on its own resources.

Using the implementation runnable interface mode reality:

 Public classRunabletestImplementsRunnable {Private intTicket = 100;  Public voidrun () { while(true){            if(Ticket > 0) {System.out.println (Thread.CurrentThread (). GetName ()+ "is saling ticket" + ticket--); }Else{                 Break; }        }    }}

its corresponding test class:

 public  class   Testdemo { public  static  void   main (string[] args) {        Runabletest r  = new   Runabletest ();         new   Thread (R). Start ();         new   Thread (R). Start ();         new   Thread (R). Start ();     new   Thread (R). Start (); }}    

run Result:

  .... Thread  -0is saling ticket 12thread  -0is saling ticket 11thread  -0is saling ticket 10thread  -0is saling ticket 9thread  -0is saling ticket 8thread  -0is saling ticket 7thread  -1is saling ticket 6thread  -1is saling ticket 5thread  -1is saling ticket 4thread  -1is Saling ticket 3thread  -1is saling ticket 2 ..... 

In the above program, four threads were created, each calling the run () method in the same ThreadTest object, accessing an instance of the variable (tickets) in the same object. This program has met our needs.

visible, there are significant benefits to implementing the Runnable interface relative to inheriting the thread class:

1, runable interface for multiple identical program code to deal with the same resource, that is, the Runnable interface is easy to achieve resource sharing, and the thread class is not suitable for resource sharing.

2, using the Runnable interface avoids the restriction of single inheritance in Java.

3, increase the robustness of the program, code can be shared by multiple threads, code and data Independent.

Java Multithreading Implementation

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.