Design a multi-threaded program as follows: Design a train ticketing simulator. If there are 100 train tickets to sell at the station, there are now 5 tickets available at the same time, with 5 threads to simulate the sale of the 5 ticket booths.
1. Required to print out the ticket sold at each ticket point
2. Tickets for the same ticket cannot be sold at each ticketing point
Package Com.hebust.java.third;
Import Java.util.Random;
public class Saleticket implements Runnable {
public int total;
public int count;
Public Saleticket () {
Total = 100;
Count = 0;
}
public void Run () {
while (Total > 0) {
Synchronized (this) {
if (Total > 0) {
try {
Thread.Sleep (800);
Thread.Sleep (New Random (). Nextint (1000));
} catch (Interruptedexception e) {
E.printstacktrace ();
}
count++;
total--;
System.out.println (Thread.CurrentThread (). GetName () + "\ t Current ticket:" + count);
}
}
}
}
public static void Main (string[] args) {
Saleticket st = new Saleticket ();
for (int i=1; i<=5; i++) {
New Thread (St, "ticket point" + i). Start ();
}
}
}
Java multithreaded implementation of simple ticketing procedures