多线程模拟火车票卖票-实现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--)+"号票已售出!");
      }
    }

  }

}

posted @ 2019-03-12 16:48  幻音坊-女帝  阅读(613)  评论(0编辑  收藏  举报