Java Multithreading implements multiple windows to sell tickets at the same time

Source: Internet
Author: User

Second way to create a thread: implementing the Runable Interface

Steps:
1, define class implementation runnable interface
2, overwrite the Run method in the Runnable interface.
Store the code that the thread will run in the Run method.

3, the thread object is established through the thread class.
4, pass the subclass object of the Runnable interface as the actual parameter to the constructor of the thread class.
Why pass the subclass object of the Runnable interface to the constructor of thread.
Because, the object that the custom run method belongs to is the subclass object of the Runnable interface.
So let the thread specify the run method of the specified object. You must specify the object to which the Run method belongs.

5, call the Start method of the thread class to open the thread and invoke the Run method of the Runnable interface subclass.

What is the difference between the way of implementation and the way of inheritance?

Implementation benefits: Avoids the limitations of single inheritance.
When you define a thread, you establish how it is implemented.

There are two different ways to differentiate:
Inherit thread: The threading code is stored inside the thread subclass Run method.

To implement runnable, the thread code has the Run method of the subclass of the interface.

Class Ticket implements runnable//extends thread{private  int tick = 100;public void run () {while (true) {if (tick>0) {System.out.println (Thread.CurrentThread (). GetName () + ".... Sale:" + tick--);}}} Class  ticketdemo{public static void Main (string[] args) {Ticket t = new Ticket (); thread T1 = new Thread (t),//Creates a thread, thread t2 = new Thread (t),//Creates a thread, thread t3 = new Thread (t);//creates a thread; threads t4 = NE W thread (t);//create a thread; T1.start (); T2.start (); T3.start (); T4.start ();/*ticket t1 = new Ticket ();//ticket t2 = new Ticket () ;//ticket t3 = new Ticket ();//ticket t4 = new Ticket (); T1.start (); T1.start (); T1.start (); T1.start (); *}}

———— from "Bi Xiangdong 25 Days"

Java Multithreading implements multiple windows to sell tickets at the same time

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.