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元”
相关分类