多线程模拟火车票卖票-实现Runnable接口
public class Demo3_Synchronized {
public static void main(String[] args) {
// TODO Auto-generated method stub
Ticket2 ticket2 = new Ticket2();
new Thread(ticket2) {}.start();
new Thread(ticket2) {}.start();
new Thread(ticket2) {}.start();
new Thread(ticket2) {}.start();
}
}
class Ticket2 implements Runnable{
private int ticket2 = 100;
@Override
public void run() {
// TODO Auto-generated method stub
synchronized (this) {
while(ticket2>0) {
System.out.println((ticket2--)+"号票已售出!");
}
}
}
}