class TestDemo {
public static void main(String[] args) {
Ticket t = new Ticket();
Thread t1 = new Thread(t, "合肥");
Thread t2 = new Thread(t, "芜湖");
t1.start();
t2.start();
}
}
class Ticket implements Runnable{
private int ticket =100;
public Ticket(int ticket) {
super();
this.ticket = ticket;
}
public Ticket() {
}
@Override
public void run() {
while (ticket !=0) {
synchronized (this)
{
if (ticket >0) {
ticket--;
System.out.println(Thread.currentThread().getName()+"站卖出一张票!还剩余"+ticket+"张票");
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
转载:https://blog.csdn.net/XinYueChangLE/article/details/101625264
查看评论