LinkedBlockingQueue 类的作用:

首先有三个关键字:
1.Linked

2.Blocking

3.Queue

1.首先是一个队列。
2.是一个阻塞队列。
3.由链表实现。

作用:

当你放一个元素的时候,当LinkedBlockingQueue 已经满了,如果调用put方法,那么你存放的线程就会阻塞,直到其他线程从队列中取出元素,这时候你就可以放进队列。但是如果调用offer方法,那么offer方法直接会返回false.不会阻塞线程。

LinkedBlockingQueue 可以指定大小,不指定大小,默认为Integer.MAX_VALUE.

当你取一个元素的时候,如果队列是空的,那么会阻塞当前线程,直到其他线程存放了数据到队列里面。

实现原理:

    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.*///如果队列已经满了 那么会一直等待其他线程调用notFull 唤醒while (count.get() == capacity) {notFull.await();}enqueue(node);c = count.getAndIncrement();if (c + 1 < capacity)//通知没有满notFull.signal();} finally {putLock.unlock();}if (c == 0)//发信号  表示当前队列已经不是非空  可以使用take 取数据了signalNotEmpty();}
 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;}
public E take() throws InterruptedException {E x;int c = -1;final AtomicInteger count = this.count;final ReentrantLock takeLock = this.takeLock;takeLock.lockInterruptibly();try {//如果当前队列数量为0  那么等待其他存放数据  放出notEmpty 信号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;
}

LinkedBlockingQueue 解析相关推荐

  1. ArrayBlockingQueue和LinkedBlockingQueue解析

    1. BlockingQueue接口简介 java.util.concurrent 包里的 BlockingQueue是一个接口, 继承Queue接口,Queue接口继承 Collection. Bl ...

  2. 面试官系统精讲Java源码及大厂真题 - 19 LinkedBlockingQueue 源码解析

    19 LinkedBlockingQueue 源码解析 更新时间:2019-10-11 17:02:21 从不浪费时间的人,没有工夫抱怨时间不够. --杰弗逊 引导语 说到队列,大家的反应可能是我从来 ...

  3. LinkedBlockingQueue源码解析(1)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.对于LinkedBlockingQueue需要掌握以下几点 创建 入队(添加元素) 出队(删除元素) 2 ...

  4. Jdk1.6 JUC源码解析(13)-LinkedBlockingQueue

    功能简介: LinkedBlockingQueue是一种基于单向链表实现的有界的(可选的,不指定默认int最大值)阻塞队列.队列中的元素遵循先入先出 (FIFO)的规则.新元素插入到队列的尾部,从队列 ...

  5. Android之DiskLruCache源码解析

    转载请标明出处: http://blog.csdn.net/hai_qing_xu_kong/article/details/73863258 本文出自:[顾林海的博客] 个人开发的微信小程序,目前功 ...

  6. Android多线程之ArrayBlockingQueue源码解析

    阻塞队列系列 Android多线程之LinkedBlockingQueue源码解析 Android多线程之SynchronousQueue源码解析 Andorid多线程之DelayQueue源码分析 ...

  7. dubbo源码解析(三十五)集群——cluster

    集群--cluster 目标:介绍dubbo中集群容错的几种模式,介绍dubbo-cluster下support包的源码. 前言 集群容错还是很好理解的,就是当你调用失败的时候所作出的措施.先来看看有 ...

  8. RocketMQ:消费端的消息消息队列负载均衡与重新发布机制源码解析

    文章目录 前言 流程解析 总结 前言 在上一篇博客中我们了解到,PullMessageService线程主要是负责从pullRequestQueue中获得拉取消息请求并进行请求处理的. PullMes ...

  9. Java 高频面试题:聊一聊 JUC 下的 LinkedBlockingQueue

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:推荐 8 个常用 Spring Boot 项目个人原创+1博客:点击前往,查看更多 本文聊一下 JUC 下的 L ...

最新文章

  1. 为什么不建议用 equals 判断对象相等?
  2. Microsoft宣称Visual Studio Installer将退役
  3. KVM — 开启嵌套虚拟化
  4. Framework中网络定位服务简介
  5. c语言凸包算法,基于C语言的凸包算法实现
  6. 怎样学操作系统?一文带你掌握核心内容
  7. 《Effective C#》读书笔记——条目14:尽量减少重复的初始化逻辑.NET资源管理
  8. ArcMap水文分析系列教程
  9. WIN7下安装IIS
  10. 逻辑覆盖:语句覆盖、判定覆盖、条件覆盖、判定/条件覆盖、组合覆盖和路径覆盖
  11. Flash动画短片制作流程注意点
  12. 影评分析第3篇 上映4天,票房7.4亿的《海王》,用数据看大片!
  13. C语言 自定义数据类型(结构体 typedef 共用体 位段 枚举)
  14. 磁珠 符号_超实用理解磁珠
  15. [任务2]安装ubuntu Linux
  16. 上海泛微面经(从Java开发到项目实施岗)
  17. HTML函数多个条件并列,countifs多个并列条件(countifs同一列2个条件
  18. c语言error语句错误,【资料】C语言错误信息中文解释
  19. 齐岳提供AIE分子N-苄基-4-溴-1, 8-蔡酰亚胺,近红外发射的BODIPY-PhOSi和BODIPY-DMA,超分子聚合物PNA-GBP·I2的合成
  20. 万兆单模模块_华为万兆单模模块 OEM光模块 全面兼容OSX010000

热门文章

  1. linux查看服务依赖关系,服务管理(1)
  2. wireshark出现rst的原因_长水口损毁,其主要原因是这三点造成的
  3. 基于单片机的倒车雷达系统设计c语言,基于AT89C2051单片机实现超声波倒车雷达系统的设计...
  4. java newsize_JVM中的-Xms -Xmx -XX:newSize -XX:MaxnewSize -Xmn -XX:PermSize -XX:MaxPermSize区别介绍...
  5. histeq函数实现直方图的均衡化和规定化
  6. vs+命令行运行带参数cpp文件
  7. python协程池操作mysql_在python中使用aiomysql异步操作mysql
  8. c语言关键字_C语言初学者必须掌握的关键字!
  9. 山西晋城学籍“失踪”解决方案:可参加高考或转职高
  10. Effective_STL 学习笔记(二十八) 了解如何通过 reverse_iterator 的 base 得到 iterator...