使用简单阻塞实现有界缓存

public class SleepyBoundedBuffer<V> extends BaseBoundedBuffer<V>{
    
    
  public SleepyBoundedBuffer(int size){
    
    
    super(size);
  }

  public void put() throws InterruptedException{
    
    
    while(true){
    
    
      synchronized(this){
    
    
        if(!isFull()){
    
    
          doPut(v);
          return;
        }
      }
      Thread.sleep(SLEEP_GRANULARITY);
    }
  }
  
  public V take() throws InterruptedException{
    
    
    while(true){
    
    
      synchronized(this){
    
    
        if(!isEmpty()){
    
    
          return doTake();
        }
      }
    Thread.sleep(SLEEP_GRANULARITY);
    }
  }
  
}

猜你喜欢

转载自blog.csdn.net/weixin_37632716/article/details/119393269