程序纠错,寻找原因,谢谢

public class maipiao implements Runnable{

    Thread wang,li;


    maipiao()
    {
        wang = new Thread(this);
        li = new Thread(this);
    }

    @Override
    public void run() {
        if(Thread.currentThread()== wang)
        {
            new saleTicket().rule(5);
        }
        else if(Thread.currentThread() == li)
        {
            new saleTicket().rule(20);
        }

    }
  public static void main(String[] args) {
    maipiao a = new maipiao();
    a.wang.start();
    a.li.start();
}
}

public class saleTicket {
    int ticket5 = 2,ticket10 = 0 ,ticket20=0;
    public synchronized  void rule(int money)
    {
        if(money == 5)
        {
            ticket5 = ticket5+1;
            System.out.println("给你票,你的钱正好");
        }
        else if(money == 20)
        { 
            while(ticket5<3){
            try {
            wait();
            } catch (InterruptedException e) {}
            }
            ticket20 = ticket20+1;
            ticket5 = ticket5 - 3;
            System.out.println("给你票,你给我20,找你15元");
        }
        notifyAll();
    }
}

为什么运行后不出现“给你票,你给我20,找你15元”


犯罪嫌疑人X
浏览 369回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java