生产者/消费者模式2

private Egg egg = new Egg();
  
  public PutEggThread(Plate plate){
   this.plate = plate;
  }
  public void run(){
   for(int i = 0; i < PUT_EGG_TIMES; i++){
    plate.putEgg(egg);
   }
  }
 }
 
 static class GetEggThread extends Thread{
  private static final int GET_EGG_TIMES = 5;
  private Plate plate;
  
  public GetEggThread(Plate plate){
   this.plate = plate;
  }
  
  public void run(){
   for(int i = 0; i < GET_EGG_TIMES; i++){
    plate.getEgg();
   }
  }
 }
 
 private static class Egg{
  
 }
 
 public static void main(String[] args) {
  try {
   Plate plate = new Plate();
   Thread putEggThread = new PutEggThread(plate);
   Thread getEggThread = new GetEggThread(plate);
   putEggThread.start();
   getEggThread.start();
   putEggThread.join();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }
}

你可能感兴趣的:(生产者)