线程基础—并发容器:LinkedBlockingQueue

本文作者:王一飞,叩丁狼高级讲师。原创文章,转载请注明出处。

#####概念
LinkedBlockingQueue按照api解释:一个基于链表而实现的有界阻塞队列。遵循先进先出原则,由队头入列,再从队尾出列。具体操作上跟ArrayBlockingQueue类似,区别在于底层维护数据上,LinkedBlockingQueue底层是一个链接,而ArrayBlockingQueue是一个数组。
外部结构
内部结构

public class LinkedBlockingQueue<E> extends AbstractQueue<E>
        implements BlockingQueue<E>, java.io.Serializable {
   

    private final AtomicInteger count = new AtomicInteger();  //队列元素个数
    private final int capacity;  //队列容器
    transient Node<E> head;  //队头
    private transient Node<E> last; //队尾

    //出列入列过程中维护现场安全的各类锁
    private final ReentrantLock takeLock = new ReentrantLock();
    private final Condition notEmpty = takeLock.newCondition();
    private final ReentrantLock putLock = new ReentrantLock();
    private final Condition notFull = putLock.newCondition();

    //队列数据节点
    static class Node<E> {
   
        E item;
        Node<E> next;
        Node(E x) {
    item = x; }
    }
}

######基本操作

public class App {
   
    public static void main(String[] args) throws InterruptedException {
   
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue(5);
        //入列
        queue.add("a");  //队列满后抛异常
        queue.put("b");//队列满后阻塞
        queue.offer("c"); //入列失败返回false
        System.out.println(queue);
        queue.put("a");
        queue.put("b");
        queue.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值