Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LinkedBlockingQueue

LinkedBlockingQueue

原创
作者头像
大学里的混子
修改于 2019-04-08 08:07:22
修改于 2019-04-08 08:07:22
33600
代码可运行
举报
文章被收录于专栏:LeetCodeLeetCode
运行总次数:0
代码可运行

LinkedBlockingQueue

节点类型

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * Linked list node class
 */
static class Node<E> {
    E item;

    /**
     * One of:
     * - the real successor Node
     * - this Node, meaning the successor is head.next
     * - null, meaning there is no successor (this is the last node)
     */
    Node<E> next;

    Node(E x) { item = x; }
}

成员变量

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/** The capacity bound, or Integer.MAX_VALUE if none */
private final int capacity;

/** Current number of elements */
private final AtomicInteger count = new AtomicInteger();

/**
 * Head of linked list.
 * Invariant: head.item == null
 */
transient Node<E> head;

/**
 * Tail of linked list.
 * Invariant: last.next == null
 */
private transient Node<E> last;

/** Lock held by take, poll, etc */
private final ReentrantLock takeLock = new ReentrantLock();

/** Wait queue for waiting takes */
private final Condition notEmpty = takeLock.newCondition();

/** Lock held by put, offer, etc */
private final ReentrantLock putLock = new ReentrantLock();

/** Wait queue for waiting puts */
private final Condition notFull = putLock.newCondition();

put()方法

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public void put(E e) throws InterruptedException {
    if (e == null) throw new NullPointerException();
    // Note: convention in all put/take/etc is to preset local var
    // holding count negative to indicate failure unless set.
    int c = -1;
    Node<E> node = new Node<E>(e);
    final ReentrantLock putLock = this.putLock;
    final AtomicInteger count = this.count;
    putLock.lockInterruptibly();
    try {
        /*
         * Note that count is used in wait guard even though it is
         * not protected by lock. This works because count can
         * only decrease at this point (all other puts are shut
         * out by lock), and we (or some other waiting put) are
         * signalled if it ever changes from capacity. Similarly
         * for all other uses of count in other wait guards.
         */
        while (count.get() == capacity) {
            notFull.await();
        }
        enqueue(node);
        c = count.getAndIncrement();
        if (c + 1 < capacity)
            notFull.signal();
    } finally {
        putLock.unlock();
    }
    if (c == 0)
        signalNotEmpty();
}

take()方法

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 public E take() throws InterruptedException {
        E x;
        int c = -1;
        final AtomicInteger count = this.count;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lockInterruptibly();
        try {
            while (count.get() == 0) {
                notEmpty.await();
            }
            x = dequeue();
            c = count.getAndDecrement();
            if (c > 1)
                notEmpty.signal();
        } finally {
            takeLock.unlock();
        }
        if (c == capacity)
            signalNotFull();
        return x;
    }

抛出异常

返回特殊值

一直阻塞

超时退出

插入方法

add

offer

put

offer(e, time, unit)

移除方法

remove

poll

take

poll(time, unit)

offer()返回false()

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public boolean offer(E e) {
    if (e == null) throw new NullPointerException();
    final AtomicInteger count = this.count;
    if (count.get() == capacity)
        return false;
    int c = -1;
    Node<E> node = new Node<E>(e);
    final ReentrantLock putLock = this.putLock;
    putLock.lock();
    try {
        if (count.get() < capacity) {
            enqueue(node);
            c = count.getAndIncrement();
            if (c + 1 < capacity)
                notFull.signal();
        }
    } finally {
        putLock.unlock();
    }
    if (c == 0)
        signalNotEmpty();
    return c >= 0;
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
加深一下BlockingQueue的认识
认识BlockingQueue BlockingQueue是一种可以阻塞线程的队列,java中对这种队列提供了方法抽象,BlockingQueue则是抽象的接口。 add:添加元素到队列里,添加成功返回true,由于容量满了添加失败会抛出IllegalStateException异常 offer:添加元素到队列里,添加成功返回true,添加失败返回false put:添加元素到队列里,如果容量满了会阻塞直到容量不满 poll:删除队列头部元素,如果队列为空,返回null。否则返回元素。
用户1105954
2018/01/12
6660
Java 阻塞队列 BlockingQueue 详解: put,add 和 offer 三个方法
java.util.concurrent public interface BlockingQueue<E> extends Queue<E>
一个会写诗的程序员
2020/04/24
1.3K0
Java 阻塞队列 BlockingQueue 详解: put,add 和 offer 三个方法
《从Java面试题来看源码》-LinkedBlockingQueue 源码分析
有界阻塞队列,使用单向链表实现,通过ReentrantLock实现线程安全,阻塞通过Condition实现,出队和入队各一把锁,不存在互相竞争 ,一种经典的生产和消费模式场景
阿提说说
2022/12/02
4930
Java LinkedBlockingQueue
继承了AbstractQueue类,实现了BlockingQueue和Serializable接口
用户7886150
2021/04/23
3340
并发队列-无界阻塞队列LinkedBlockingQueue原理探究
前面介绍了使用CAS实现的非阻塞队列ConcurrentLinkedQueue,下面就来介绍下使用独占锁实现的阻塞队列LinkedBlockingQueue的实现
加多
2018/09/06
1K0
并发队列-无界阻塞队列LinkedBlockingQueue原理探究
LinkedBlockingQueue 核心源码解析
LinkedBlockingQueue - 单链表实现的阻塞队列。该队列按 FIFO(先进先出)排序元素,新元素从队列尾部插入,从队首获取元素.是深入并发编程的基础数据结构.
JavaEdge
2020/04/19
4510
LinkedBlockingQueue 核心源码解析
LinkedBlockingQueue源码解析
前面我们分析了ArrayBlockingQueue,今天我们接着来对LinkedBlockingQueue的源码进行解析。本文首先会对LinkedBlockingQueue的源码进行解析,接着会介绍ArrayBlockingQueue和LinkedBlockingQueue的区别。
码农飞哥
2021/08/18
3690
Java - LinkedBlockingQueue的阻塞实现
LinkedBlockingQueue是BlockingQueue的链表实现,他的阻塞体现在put和take方法上,下面将通过源码介绍如何LinkedBlockingQueue是如何实现的阻塞队列。
夹胡碰
2021/01/06
9400
JDK容器学习之Queue:LinkedBlockingQueue
基于链表阻塞队列LinkedBlockingQueue 基于链表的无边界阻塞队列,常用与线程池创建中作为任务缓冲队列使用 I. 底层数据结构 先看一下内部定义,与 ArrayBlockingQueue做一下对比,顺带看下这两者的区别及不同的应用场景 /** 队列的容量, or Integer.MAX_VALUE if none */ private final int capacity; /** 队列中实际的个数 */ private final AtomicInteger count = new At
一灰灰blog
2018/02/06
7660
连LinkedBlockingQueue源码都没看过,我怎么敢给你offer?
LinkedBlockingQueue - 单链表实现的阻塞队列。该队列按 FIFO(先进先出)排序元素,新元素从队列尾部插入,从队首获取元素.是深入并发编程的基础数据结构.
JavaEdge
2020/05/26
6800
11.并发包阻塞队列之LinkedBlockingQueue
jdk1.7.0_79   在上文《10.并发包阻塞队列之ArrayBlockingQueue》中简要解析了ArrayBlockingQueue部分源码,在本文中同样要介绍的是Java并发包中的阻塞队列LinkedBlockingQueue。ArrayBlockingQueue队列是由数组实现,而LinkedBlockingQueue队列的实现则是链表(单向链表)实现,所以在LinkedBlockingQueue有一个Node内部类来表示链表的节点。 static final class Node<E
用户1148394
2018/01/12
8260
java进阶|LinkedBlockingQueue源码分析
现在是2020/05/18:23:12分,是的,马上就要到凌晨了,然而我才开始写这篇文章,为什么这么晚写这篇文章,不困吗,或许是,或许不是,其实在我刷完抖音之后脑海里想的就是分析一下LinkedBlockingQueue的源码吧,至于为什么要这么晚还去分析,有这个必要吗,或许是自己喜欢这个点有点久了。
码农王同学
2020/06/04
4180
LinkedBlockingQueue 原理
LinkedBlockingQueue 是 Java 中用于实现线程安全队列的类。它是一个基于链接节点的阻塞队列,并且在队列为空时,获取元素的线程会阻塞;当队列满时,存储元素的线程会阻塞。LinkedBlockingQueue 的使用方法如下:
一个风轻云淡
2023/10/15
1480
LinkedBlockingQueue 原理
LinkedBlockingQueue源码解析
java.util.concurrent.LinkedBlockingQueue是一个底层为单向链表的,有界的,FIFO阻塞队列;访问和移除操作是在队头,添加操作在队尾进行,并且使用不同的锁进行保护。
IT云清
2019/01/22
9230
死磕 java集合之LinkedBlockingQueue源码分析
(3)LinkedBlockingQueue相比ArrayBlockingQueue有什么改进?
彤哥
2019/07/08
3370
JDK源码分析-LinkedBlockingQueue
前文「JDK源码分析-ArrayBlockingQueue」分析了 ArrayBlockingQueue 的代码实现,LinkedBlockingQueue 也是阻塞队列的实现。与前者不同的是,后者内部是由链表实现的。
WriteOnRead
2019/08/16
4060
LinkedBlockingQueue源码学习
采用线程池和阻塞队列实现生产/消费者模型。其中LinkedBlockingQueue是阻塞队列,同时线程安全,其特点:
路行的亚洲
2020/07/21
3800
阿里P9整理Java 高频面试题聊一聊 JUC 下的 LinkedBlockingQueue
本文聊一下 JUC 下的 LinkedBlockingQueue 队列,先说说 LinkedBlockingQueue 队列的特点,然后再从源码的角度聊一聊 LinkedBlockingQueue 的主要实现~
马士兵的朋友圈
2020/07/31
4390
深入理解队列:LinkedBlockingQueue源码深度解析
LinkedBlockingQueue的命名很规范,基本上是“闻其名,知其意”——链表阻塞队列,由此名称可知,该队列底层的数据结构是链表,并且该队列是可阻塞的。我们从IDEA中将LinkedBlockingQueue的类之间的关系截图如下所示:
itlemon
2020/04/03
6660
深入理解队列:LinkedBlockingQueue源码深度解析
聊聊 JDK 阻塞队列源码(ReentrantLock实现)
项目中用到了一个叫做 Disruptor 的队列,今天楼主并不是要介绍 Disruptor 而是想巩固一下基础扒一下 JDK 中的阻塞队列,听到队列相信大家对其并不陌生,在我们现实生活中队列随处可见,最经典的就是去银行办理业务等。 当然在计算机世界中,队列是属于一种数据结构,队列采用的FIFO(first in firstout),新元素(等待进入队列的元素)总是被插入到尾部,而读取的时候总是从头部开始读取。在计算中队列一般用来做排队(如线程池的等待排队,锁的等待排队),用来做解耦(生产者消费者模式),异步等等。
haifeiWu
2018/09/11
3510
相关推荐
加深一下BlockingQueue的认识
更多 >
加入讨论
的问答专区 >
1KOL擅长5个领域
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
    本文部分代码块支持一键运行,欢迎体验
    本文部分代码块支持一键运行,欢迎体验