public class Seller extends Thread{
public static Integer ticket = 10;
private String sellername;
public Seller(String sellername) {
super();
this.sellername = sellername;
}
@Override
public void run() {
while (ticket > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (Constants.obj) {
if (ticket > 0) {
ticket--;
System.out.println(sellername + "卖出一张票,还剩下" + ticket + "张");
}
}
}
}
}
public class TextMin5 {
public static void main(String[] args) {
Seller s1 = new Seller("张三");
Seller s2 = new Seller("李四");
Seller s3 = new Seller("王五");
s1.start();
s2.start();
s3.start();
}
}
public interface Constants {
Object obj = new Object();
}