Define a class to implement the Runnable interface, define a ticketing method that needs to be synchronized, and then rewrite the Run method to call the ticketing sale method
Class Saleticket implements runnable{
private int tickets =;
Private synchronized void sale () {
if (Tickets > 0) {
System.out.println (Thread.CurrentThread (). GetName () + " Sell the first "+ (tickets--) +" Ticket ");
try{
Thread.Sleep (MB);
catch (Interruptedexception e) {
e.printstacktrace ();
}}} public void Run () {while
(Tickets > 0) {
sale ();
}}} public class Javatest {public
static void Main (string[] args) {
Saleticket st = new Saleticket ();
thread T1 = new Thread (ST, "one Window");
Thread t2 = new Thread (St, "No. second Window");
thread t3 = new Thread (St, "No. third Window");
thread T4 = new Thread (St, "No. Fourth Window");
T1.start ();
T2.start ();
T3.start ();
T4.start ();
}