java 多线程阻塞队列 与 阻塞方法与和非阻塞方法
Queue是什么
队列,是一种数据结构。除了优先级队列和LIFO队列外,队列都是以FIFO(先进先出)的方式对各个元素进行排序的。无论使用哪种排序方式,队列的头都是调用remove()或poll()移除元素的。在FIFO队列中,所有新元素都插入队列的末尾。队列都是线程安全的,内部已经实现安全措施,不用我们担心
Queue中的方法
Queue中的方法不难理解,6个,每2对是一个也就是总共3对。看一下JDK API就知道了:

注意一点就好,Queue通常不允许插入Null,尽管某些实现(比如LinkedList)是允许的,但是也不建议。
ArrayBlockingQueue:基于数组实现的一个阻塞队列,在创建ArrayBlockingQueue对象时必须制定容量大小。并且可以指定公平性与非公平性,默认情况下为非公平的,即不保证等待时间最长的队列最优先能够访问队列。
LinkedBlockingQueue:基于链表实现的一个阻塞队列,在创建LinkedBlockingQueue对象时如果不指定容量大小,则默认大小为Integer.MAX_VALUE。
PriorityBlockingQueue:以上2种队列都是先进先出队列,而PriorityBlockingQueue却不是,它会按照元素的优先级对元素进行排序,按照优先级顺序出队,每次出队的元素都是优先级最高的元素。注意,此阻塞队列为无界阻塞队列,即容量没有上限(通过源码就可以知道,它没有容器满的信号标志),前面2种都是有界队列。
DelayQueue:基于PriorityQueue,一种延时阻塞队列,DelayQueue中的元素只有当其指定的延迟时间到了,才能够从队列中获取到该元素。DelayQueue也是一个无界队列,因此往队列中插入数据的操作(生产者)永远不会被阻塞,而只有获取数据的操作(消费者)才会被阻塞。
注意:
1、必须要使用take()方法在获取的时候达成阻塞结果
2、使用poll()方法将产生非阻塞效果
public class ArrayBlockingQueue<E> extends AbstractQueue<E>implements BlockingQueue<E>, java.io.Serializable {private static final long serialVersionUID = -817911632652898426L;/** The queued items */private final E[] items;/** items index for next take, poll or remove */private int takeIndex;/** items index for next put, offer, or add. */private int putIndex;/** Number of items in the queue */private int count;/** Concurrency control uses the classic two-condition algorithm* found in any textbook.*//** Main lock guarding all access */private final ReentrantLock lock;/** Condition for waiting takes */private final Condition notEmpty;/** Condition for waiting puts */private final Condition notFull;}
public ArrayBlockingQueue(int capacity) {}public ArrayBlockingQueue(int capacity, boolean fair) {}public ArrayBlockingQueue(int capacity, boolean fair,Collection<? extends E> c) {}
public void put(E e) throws InterruptedException {if (e == null) throw new NullPointerException();final E[] items = this.items;final ReentrantLock lock = this.lock;lock.lockInterruptibly();try {try {while (count == items.length)notFull.await();} catch (InterruptedException ie) {notFull.signal(); // propagate to non-interrupted threadthrow ie;}insert(e);} finally {lock.unlock();}}
private void insert(E x) {items[putIndex] = x;putIndex = inc(putIndex);++count;notEmpty.signal();}
public E take() throws InterruptedException {final ReentrantLock lock = this.lock;lock.lockInterruptibly();try {try {while (count == 0)notEmpty.await();} catch (InterruptedException ie) {notEmpty.signal(); // propagate to non-interrupted threadthrow ie;}E x = extract();return x;} finally {lock.unlock();}}
private E extract() {final E[] items = this.items;E x = items[takeIndex];items[takeIndex] = null;takeIndex = inc(takeIndex);--count;notFull.signal();return x;}
public class Test {private int queueSize = 10;private PriorityQueue<Integer> queue = new PriorityQueue<Integer>(queueSize);public static void main(String[] args) {Test test = new Test();Producer producer = test.new Producer();Consumer consumer = test.new Consumer();producer.start();consumer.start();}class Consumer extends Thread{@Overridepublic void run() {consume();}private void consume() {while(true){synchronized (queue) {while(queue.size() == 0){try {System.out.println("队列空,等待数据");queue.wait();} catch (InterruptedException e) {e.printStackTrace();queue.notify();}}queue.poll(); //每次移走队首元素queue.notify();System.out.println("从队列取走一个元素,队列剩余"+queue.size()+"个元素");}}}}class Producer extends Thread{@Overridepublic void run() {produce();}private void produce() {while(true){synchronized (queue) {while(queue.size() == queueSize){try {System.out.println("队列满,等待有空余空间");queue.wait();} catch (InterruptedException e) {e.printStackTrace();queue.notify();}}queue.offer(1); //每次插入一个元素queue.notify();System.out.println("向队列取中插入一个元素,队列剩余空间:"+(queueSize-queue.size()));}}}}}
public class Test {private int queueSize = 10;private ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(queueSize);public static void main(String[] args) {Test test = new Test();Producer producer = test.new Producer();Consumer consumer = test.new Consumer();producer.start();consumer.start();}class Consumer extends Thread{@Overridepublic void run() {consume();}private void consume() {while(true){try {queue.take();System.out.println("从队列取走一个元素,队列剩余"+queue.size()+"个元素");} catch (InterruptedException e) {e.printStackTrace();}}}}class Producer extends Thread{@Overridepublic void run() {produce();}private void produce() {while(true){try {queue.put(1);System.out.println("向队列取中插入一个元素,队列剩余空间:"+(queueSize-queue.size()));} catch (InterruptedException e) {e.printStackTrace();}}}}}
java 多线程阻塞队列 与 阻塞方法与和非阻塞方法的更多相关文章
- 【java多线程】队列系统之说说队列Queue
转载:http://benjaminwhx.com/2018/05/05/%E8%AF%B4%E8%AF%B4%E9%98%9F%E5%88%97Queue/ 1.简介 Queue(队列):一种特殊的 ...
- 使用goroutine+channel和java多线程+queue队列的方式开发各有什么优缺点?
我感觉很多项目使用java或者c的多线程库+线程安全的queue数据结构基本上可以实现goroutine+channel开发能达到的需求,所以请问一下为什么说golang更适合并发服务端的开发呢?使用 ...
- “全栈2019”Java多线程第二十八章:公平锁与非公平锁详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- 是否可以从一个static方法内部调用非static方法?
不可以.静态成员不能调用非静态成员. 非static方法属于对象,必须创建一个对象后,才可以在通过该对象来调用static方法.而static方法调用时不需要创建对象,通过类就可以调用该方法.也就是说 ...
- python3 封装之property 多态 绑定方法classmethod 与 非绑定方法 staticmethod
property 特性 什么是特性property property 是一种特殊的属性,访问它时会执行一段功能(函数),然后返回值 例如 BMI指数(bmi是计算而来的,但很明显它听起来像是一个属性而 ...
- Java多线程:队列与阻塞队列
1. 什么是阻塞队列 阻塞队列(BlockingQueue)是 Java 5 并发新特性中的内容,阻塞队列的接口是 java.util.concurrent.BlockingQueue,它提供了两个附 ...
- java多线程之队列
1.注:先不看阻塞与否,这ReentrantLock的使用方式就能说明这个类是线程安全类. 2.线程安全的类,BlockingQueue,ConcurrentLinkedQueue.这些都是线程安全的 ...
- 【java多线程】队列系统之LinkedBlockingQueue源码
转载:https://blog.csdn.net/tonywu1992/article/details/83419448 http://benjaminwhx.com/archives/ 1.简介 上 ...
- java并发编程(8)原子变量和非阻塞的同步机制
原子变量和非阻塞的同步机制 一.锁的劣势 1.在多线程下:锁的挂起和恢复等过程存在着很大的开销(及时现代的jvm会判断何时使用挂起,何时自旋等待) 2.volatile:轻量级别的同步机制,但是不能用 ...
随机推荐
- 新浪API及调用案例
百度分享:http://pan.baidu.com/s/1uSmku
- 使用 Jenkins 实现软件开发的持续集成
转自:http://www.ibm.com/developerworks/cn/java/j-lo-jenkinsintegrate/ Jenkins 是一种易于使用的持续集成系统,它可以使开发者从繁 ...
- linux利器expect的使用
1.什么是expect在做系统管理时,我们很多时候需要输入密码,例如:连接 ssh,连接ftp,那么如何能做到不输入密码,我们需要有一个工具,能代替我们实现与终端的交互,它能够代替我们实现与终端的交互 ...
- IntelliJ IDEA java项目导入jar包,打jar包
一.导入 1.java项目在没有导入该jar包之前,如图: 2.点击 File -> Project Structure(快捷键 Ctrl + Alt + Shift + s),点击Proje ...
- linux创建新用户及权限
在Linux中添加普通新用户 ,超级用户(也称为“root”)是一个具有修改系统中任何文件权力的特别账号.在日常工作中,最好不要使用超级用户账号进入系统,因为任何错误操作都可能导致巨大的损失.由于超级 ...
- 用Visual Studio 2010开发Android应用
在开发你的第一个Android应用程序之前,你应该先检查一下是否安装了Android SDK,以及是否创建好了Android模拟器(AVD),如果有不清楚的地方,请先看我以前发布的这篇文章“Andro ...
- linux文件系统管理的工作原理
一.系统在初始化时如何识别硬盘 1.系统初始时根据MBR的信息来识别硬盘,其中包括了一些执行文件就来载入系统,这些执行文件就是MBR里前面446bytes里的boot loader 程式,而后面的16 ...
- vue中如何实现数据的双向绑定
vue中如何实现数据的双向绑定 实现视图变化数据跟着变:分两步,上面get中的为第二步(即再次读取的时候会调用get方法得到之前设置的值,以此来实现动态改变) 由于直接写obj.name = this ...
- 使用国内镜像composer安装laravel
1.安装 Laravel,创建blog项目 首先先说一下直接安装的方法,要想使用这个方法,首先要FQ(这是我们的基本国情决定的).这样的安装方式有两种: ①.全局安装 使用命令Laravel Inst ...
- unity, Find References In Scene
材质,脚本,shader等都可以通过Find References In Scene查看引用情况,如图. 当对一个文件点击Find References In Scene后,搜索命令会显示到Scene ...