The thoughts of multi-thread ticketing system

Source: Internet
Author: User
Tags ticket

Package exception exercise;
Class Seel implements runnable{
private String name;
private int ticket=100;
Seel (String name) {
This.name=name;
}

public void Run () {
while (true) {
if (ticket>0) {
System.out.println (Thread.CurrentThread (). GetName () + "ticketing" + "..." + "Balance of" + (--ticket));
}
}
}
}
public class Text26 {
public static void Main (string[] args) {

Seel s=new Seel ("Ticket machine 1");

/*seel s2=new Seel ("Ticket Machine 2");
Seel s3=new Seel ("Ticket machine 3");
Seel s4=new Seel ("Ticket machine 4");
Seel s5=new Seel ("Ticket machine 5");
*/
Thread t1=new thread (S);
Thread t2=new thread (S);
Thread t3=new thread (S);
Thread t4=new thread (S);
Thread t5=new thread (S);
T1.start ();
T2.start ();
T3.start ();
T4.start ();
In the ticketing system we can not new out many sub-class, because they are relatively independent, multi-threaded opening is not interfering with each other,
And the total number of tickets in the ticket machine is shared, all of us to use the same sub-class, and then the subclass as
Parameters are passed to the tread class, and then let tread new out multiple objects to realize the phenomenon of multiple ticket machines.
This way, although we open the thread's multithreaded pattern, the Seel class is unique because it passes in the parameters, which causes the methods and properties in this class to share the common effect.
}
}

........................

Class Seel implements runnable{
private String name;
private int ticket=100;
Seel (String name) {
This.name=name;
}

public void Run () {
while (true) {
if (ticket>0) {
System.out.println (Thread.CurrentThread (). GetName () + "ticketing" + "..." + "Balance of" + (--ticket));
}
}
}
}
public class Text29 {
public static void Main (string[] args) {

Seel s=new Seel ("ticketing");
Thread T1=new thread (S, "Ticket machine 1");
Thread T2=new thread (S, "Ticket Machine 2");
Thread T3=new thread (S, "Ticket machine 3");
Thread T4=new thread (S, "Ticket machine 4");
Thread T5=new thread (S, "Ticket machine 5");
T1.start ();
T2.start ();
T3.start ();
T4.start ();

In this improvement I found that the thread constructor has a type of thread (Runnable target, string name) {}; The string name inside is passed the message,
Then this message will overwrite the one that's automatically named in the thread.
}
}

The thoughts of multi-thread ticketing system

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.