LinkedBlockingQueue 的API中,从队列中获取元素,有以下几个方法:

1、take():原文:Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.

翻译完:从队列中取出元素E,如果队列为空,则阻塞该线程直到队列不为空拿出元素E位置;

这样可能造成的情况是:在生产者消费中模式中,如果生产者已经生产完毕了,消费中消费完毕后,队列为空,此时用take()方法会阻塞,从而导致线程无法关闭,

表现在运行后,eclipse中的terminae一直为红色;

2、poll():原文:Retrieves and removes the head of this queue, or returns null if this queue is empty.

翻译为:取出并删除队列中的首元素,如果队列为空,则返回null,不进行阻塞。

相比于take()可以根据其他条件的判断,关闭线程,不会出现上述状态。

举个例子:生产消费中,生产者一直生产直到生产完毕后,通知消费者,我生产完了,消费者通过pool()方法一直取出元素E,直到某次,从队列取出来的=null,

且此时收到生产者说我生产完了,那么你就可以顺利关闭消费者线程了。

3、poll(long timeout, TimeUnit unit):Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.

翻译为:取出后删除头元素E,如果取不到则等到timeout时间在取,还是取不到就返回null;

比如得到10秒,timeout为10.TimeUnit.Second

4、peek():Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

翻译:取出第一个元素但是不删除它,没有就返回null

ConcurrentLinkedQueue:队列中提供了上面的poll()、peek()方法,因此用ConcurrentLinkedQueue队列不会进行阻塞,

两者都是线程安全的。

LinkedBlockingQueue 与ConcurrentLinkedQueue队列的不同与同的更多相关文章

  1. LinkedBlockingQueue 和 ConcurrentLinkedQueue的区别

    1. 简单的开篇 LinkedBlockingQueue 和 ConcurrentLinkedQueue 是 Java 高并发场景中最常使用的队列.尽管这两个队列经常被用作并发场景的数据结构,但它们之 ...

  2. 阻塞队列LinkedBlockingQueue和并发队列ConcurrentLinkedQueue

    LinkedBlockingQueue: public class LinkedBlockingQueue<E> extends AbstractQueue<E> implem ...

  3. LinkedBlockingQueue和ConcurrentLinkedQueue详细用法

    1.LinkedBlockingQueue<E>:java.util.concurrent API中的解释: public class LinkedBlockingQueue<E&g ...

  4. java LinkedBlockingQueue和ConcurrentLinkedQueue的区别

    实现上看,两者都继承于AbstractQueue,但是ConcurrentLinkedQueue实现了Queue,而LinkedBlockingQueue实现了BlockingQueue,Blocki ...

  5. (原创)JAVA阻塞队列LinkedBlockingQueue 以及非阻塞队列ConcurrentLinkedQueue 的区别

    阻塞队列:线程安全 按 FIFO(先进先出)排序元素.队列的头部 是在队列中时间最长的元素.队列的尾部 是在队列中时间最短的元素.新元素插入到队列的尾部,并且队列检索操作会获得位于队列头部的元素.链接 ...

  6. 11.并发包阻塞队列之LinkedBlockingQueue

    在上文<10.并发包阻塞队列之ArrayBlockingQueue>中简要解析了ArrayBlockingQueue部分源码,在本文中同样要介绍的是Java并发包中的阻塞队列LinkedB ...

  7. 并发队列之LinkedBlockingQueue

    上一篇我们看了一下这个队列ConcurrentLinkedQueue,那就是一个无界非阻塞链表,我们这次来看看LinkedBlockingQueue,这个队列看名字就知道是一个阻塞式队列(也就是一个单 ...

  8. 并发编程(九)—— Java 并发队列 BlockingQueue 实现之 LinkedBlockingQueue 源码分析

    LinkedBlockingQueue 在看源码之前,通过查询API发现对LinkedBlockingQueue特点的简单介绍: 1.LinkedBlockingQueue是一个由链表实现的有界队列阻 ...

  9. 【java多线程】队列系统之LinkedBlockingQueue源码

    转载:https://blog.csdn.net/tonywu1992/article/details/83419448 http://benjaminwhx.com/archives/ 1.简介 上 ...

随机推荐

  1. 观《phonegap第三季 angularjs+ionic视频教程 实时发布》学习笔记(三)

    十五.ionic路由 1.ionic中内联模板介绍 使用内联模板内联模板的使用,常见的有几种情况.(1) 使用ng-include指令可以利用ng-include指令在HTML中直接使用内联模板,例如 ...

  2. 巨蟒python全栈开发-第4天 列表&元组&range

    今日内容大纲 1. 什么是列表 定义: 能装对象的对象 在python中使用[]来描述列表, 内部元素用逗号隔开. 对数据类型没有要求 列表存在索引和切片. 和字符串是一样的. 2. 相关的增删改查操 ...

  3. Java Json API:Gson使用简单入门

    GSON是Google开发的Java API,用于转换Java对象和Json对象.本文讨论并提供了使用API的简单代码示例.更多关于GSON的API可以访问:http://sites.google.c ...

  4. Using Swift with Cocoa and Objective-C--在同个project中使用Swift和在同个project中

    http://www.cocoachina.com/newbie/basic/2014/0605/8688.html watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5u ...

  5. 48dp rhythm

  6. 关于shared pool的深入探讨(二)【转载】

    关于shared pool的深入探讨(二)   作者:eygle |English [转载时请标明出处和作者信息]|[恩墨学院 OCM培训传DBA成功之道]链接:http://www.eygle.co ...

  7. linux进程内存到底怎么看 剖析top命令显示的VIRT RES SHR值

    引 言: top命令作为Linux下最常用的性能分析工具之一,可以监控.收集进程的CPU.IO.内存使用情况.比如我们可以通过top命令获得一个进程使用了多少虚拟内存(VIRT).物理内存(RES). ...

  8. Ubuntu 13.04安装mysql workbench

    1.如果你没有更换源,是主服务器的官网源,这时候你可以直接用sudo apt-get install mysql-workbench来安装(前提是已经装好mysql相关服务) 2.如果你第一条装不了, ...

  9. oracle ORA-01704: string literal too long

    导出数据时,在SQL拼接处,提示 oracle ORA-01704: string literal too long sql:  WITH already_in AS (SELECT distinct ...

  10. SpringMVC学习大纲

    PartA: 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合MyBatis 5.参数绑定 a) SpringMVC ...