栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA并发编程有界缓存的实现详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA并发编程有界缓存的实现详解

JAVA并发编程有界缓存的实现

1、有界缓存的基类



package cn.xf.cp.ch14;


public class baseBoundedBuffer
{
  private final V[] buf;
  private int tail;
  private int head;
  private int count;
  
  public baseBoundedBuffer(int capacity)
  {
    //初始化数组
    this.buf = (V[]) new Object[capacity];
  }
  
  //放入一个数据,final方法无法被重写
  protected synchronized final void doPut(V v)
  {
    buf[tail] = v;
    if(++tail == buf.length)
    {
      tail = 0;
    }
    //插入一个方法,总量++
    ++count;
  }
  
  
  protected synchronized final V doTake()
  {
    V v = buf[head];
    buf[head] = null;
    if(++head == buf.length)
    {
      head = 0;
    }
    --count;
    return v;
  }
  
  //通过对count的判断,来确定数组是否是满的
  public synchronized final boolean isFull()
  {
    return count == buf.length;
  }
  
  public synchronized final boolean isEmpty()
  {
    return count == 0;
  }
}

2、判定前提条件再执行操作



package cn.xf.cp.ch14;


public class GrumpyBoundedBuffer extends baseBoundedBuffer
{

  public GrumpyBoundedBuffer(int size)
  {
    super(size);
  }
  
  public synchronized void put(V v) throws Exception
  {
    //如果是满的队列,就无法插入新的元素
    if(this.isFull())
    {
      throw new Exception("队列超出");
    }
    this.doPut(v);
  }
  
  //同理,队列为空的就无法取出新的元素
  public synchronized V take() throws Exception
  {
    if(this.isEmpty())
    {
      throw new Exception("队列中无元素");
    }
    
    return this.doTake();
  }

}

3、通过轮询与休眠来实现简单的阻塞



package cn.xf.cp.ch14;


public class SleepyBoundedBuffer extends baseBoundedBuffer
{
  //2s
  private static final long SLEEP_GRANULARITY = 2000;

  public SleepyBoundedBuffer(int capacity)
  {
    super(capacity);
  }
  
  //放入队列的时候
  public void put(V v) throws InterruptedException
  {
    while(true)
    {
      //这里不对循环上锁,不然这个锁就无法释放了,不对休眠上锁,休眠上锁,在休眠的时候别人也无法操作,永远都不可能有元素出去
      synchronized (this)
      {
 //如果队列不是满的,那么就放入元素
 if(!this.isFull())
 {
   this.doPut(v);
   return;
 }
      }
      //否则休眠,退出cpu占用
      Thread.sleep(SLEEP_GRANULARITY);
    }
  }
  
  public V take() throws InterruptedException
  {
    while(true)
    {
      //这里不对循环上锁,不然这个锁就无法释放了,不对休眠上锁,休眠上锁,在休眠的时候别人也无法操作,永远都不可能有新的元素进来
      synchronized(this)
      {
 //如果数组部位空,那么就可以取出数据
 if(!this.isEmpty())
 {
   return this.doTake();
 }
 //如果队列为空,休眠几秒再试
      }
      Thread.sleep(SLEEP_GRANULARITY);
    }
  }
  
}

4、条件队列



package cn.xf.cp.ch14;


public class BoundedBuffer extends baseBoundedBuffer
{

  public BoundedBuffer(int capacity)
  {
    super(capacity);
  }
  
  
  public synchronized void put(V v) throws InterruptedException
  {
    while(this.isFull())
    {
      //这里挂起程序,会释放锁
      this.wait();
    }
    //如果队列不为满的,那么程序被唤醒之后从新获取锁
    this.doPut(v);
    //执行结束,唤醒其他队列
    this.notifyAll();
  }
  
  public synchronized V take() throws InterruptedException
  {
    while(this.isEmpty())
    {
      this.wait();
    }
    V v = this.doTake();
    this.notifyAll();
    return v;
  }
  
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号