首页 > PHP资讯 > JAVA培训 > 同步方法中使用wait(),notify(),notifyAll()

同步方法中使用wait(),notify(),notifyAll()

JAVA培训

同步方法中使用wait(),notify(),notifyAll()

       wait(),notify(),notifyAll()方法都是Object类中的final方法,被所有类继承,不能重写。当一个线程使用的同步方法中用到某个变量。而次变量需要其它线程修改后才能符合本线程的需要,那么可以在本线程中使用wait()方法。它中断线程的执行,使本线程处于等待状态,让出CPU使用权,并让其它线程使用这个同步方法,其它线程在使用这个同步方法时若不需要等待,那么它使用完这个同步方法后,要用notifyAll()方法通知所有的由于使用该同步方法而处于等待的线程结束等待。层中断的线程就会重新排队等待CPU资源,以便从刚才中断处继续执行这个同步方法。(如果用notify()方法,那么只是通知处于等待状态的线程中的某一个结束等待)

三人排队买票,票价5,初始售票员只有一张5.a:20;b:10;C:5

* @author Young

* class TestConmunicate

*/

public class TicketSell {
       int fiveNum = 1,tenNum = 0,twentyNum = 0;
       TicketSell() {
       }
       public synchronized void sellTicket(int receiveMoney){
              if (receiveMoney == 5) {
                     fiveNum += 1;
                     System.out.println(Thread.currentThread().getName()+" 给我5块钱,这是您的入场券\n");
              } else if (receiveMoney == 10){
                     while (fiveNum < 1) {
                            try {
                                   System.out.println(Thread.currentThread().getName()+" 靠边站");
                                   wait();
                                   System.out.println(Thread.currentThread().getName()+" 结束等待");
                            } catch (Exception e) {
                                   e.printStackTrace();
                            }
                     }
                     fiveNum -= 1;
                     tenNum += 1;
                     System.out.println(Thread.currentThread().getName()+" 给我10元钱,找你5块,这是你的入场券!");
              }else if (receiveMoney == 20) {
                     while (fiveNum < 1 || tenNum < 1) {
                            try {
                                   System.out.println(Thread.currentThread().getName()+" 靠边站");
                                   //wait();
                                   Thread.currentThread().wait();
                                   System.out.println(Thread.currentThread().getName()+" 结束等待!");
                            } catch (Exception e) {
                                   e.printStackTrace();
                            }
                     }
                     fiveNum -= 1;
                     tenNum -= 1;
                     twentyNum += 1;
                     System.out.println(Thread.currentThread().getName()+" 给我20元,找你15元,这是你的入场券!");
              }
              notify();
       }
}

 

public class Cinamal implements Runnable{
   Thread a,b,c;
   TicketSell sell;
   public Cinamal() {
      a = new Thread(this);
      b = new Thread(this);
      c = new Thread(this);
      sell = new TicketSell();
      a.setName("A");
      b.setName("B");
      c.setName("C");
   }
   public void run() {
      if (Thread.currentThread() == a) {
        sell.sellTicket(10);
      } else if (Thread.currentThread() == b) {
        sell.sellTicket(5);
      } else {
        sell.sellTicket(20);
      }
   }
}

public class TestSellTicket {
   public TestSellTicket() {
   }
   public static void main(String[] args) {     
      Cinamal test = new Cinamal();
      test.a.start();
      test.b.start();
      test.c.start();
   }
}

常州java培训

本文由欣才IT学院整理发布,未经许可,禁止转载。