package com.heima.practice;
public class Practice08 {
public static void main(String[] args) {
TicketsSeller t1 = new TicketsSeller();
TicketsSeller t2 = new TicketsSeller();
TicketsSeller t3 = new TicketsSeller();
TicketsSeller t4 = new TicketsSeller();
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t4.setName("窗口4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class TicketsSeller extends Thread{
private static int tickets = 100;
static Demo1 d = new Demo1();
public TicketsSeller() {
super();
}
public TicketsSeller(String name) {
super(name);
}
public void run () {
while (true) {
synchronized (d) {
if (tickets <= 0) {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName() + "..这是第" + tickets-- + "号票");
}
}
}
}
class Demo1 {} |