1) BlockingQueue Intro
A queue is a data structure with two fundamental operations: to add an element to the tail of the queue and remove an element from the head.
That is, the queue follows FIFO(First In First Out) discipline.
A blocking queue is a queue that blocks when you try to dequeue from it and the queue is empty, or when you try to enqueue to it and the queue is full.
2) Simple ArrayBlockQueue Demo
package edu.xmu.thread; import java.util.ArrayList; import java.util.List; public class MyArrayBlockingQueue<E> { private List<E> queue; private int capacity; public MyArrayBlockingQueue(int capacity) { queue = new ArrayList<E>(capacity); this.capacity = capacity; } public synchronized void enqueue(E item) throws InterruptedException { System.out.println(String.format( "Thread: [%s] attempts to enqueue. Current queue size: [%d]", Thread.currentThread(), queue.size())); while (queue.size() == capacity) { System.out.println(String.format( "Thread [%s] is waiting for enqueue.", Thread.currentThread())); wait(); //When current thread is notified, and jumped out of while loop, it still have to attempt to acquire the lock again } if (queue.isEmpty()) { notifyAll(); } queue.add(item); System.out.println(String.format("Thread [%s] executed enqueue.", Thread.currentThread())); } public synchronized E dequeue() throws InterruptedException { System.out.println(String.format( "Thread: [%s] attempts to dequeue. Current queue size: [%d]", Thread.currentThread(), queue.size())); while (queue.size() == 0) { System.out.println(String.format( "Thread [%s] is waiting for dequeue.", Thread.currentThread())); wait(); } if (queue.size() == capacity) { notifyAll(); } E item = queue.remove(0); System.out.println(String.format("Thread [%s] executed dequeue.", Thread.currentThread())); return item; } }
3) Producer & Consumer Using BlockingQueue
package edu.xmu.thread; public class BlockingQueueTest { private static final int INITIAL_CAPACITY = 10; public static void main(String[] args) { MyArrayBlockingQueue<Food> foodQueue = new MyArrayBlockingQueue<>( INITIAL_CAPACITY); Thread producer1 = new Thread(new FoodProducer(foodQueue)); Thread producer2 = new Thread(new FoodProducer(foodQueue)); Thread producer3 = new Thread(new FoodProducer(foodQueue)); Thread consumer1 = new Thread(new FoodConsumer(foodQueue)); Thread consumer2 = new Thread(new FoodConsumer(foodQueue)); Thread consumer3 = new Thread(new FoodConsumer(foodQueue)); Thread consumer4 = new Thread(new FoodConsumer(foodQueue)); Thread consumer5 = new Thread(new FoodConsumer(foodQueue)); Thread consumer6 = new Thread(new FoodConsumer(foodQueue)); producer1.start(); producer2.start(); producer3.start(); consumer1.start(); consumer2.start(); consumer3.start(); consumer4.start(); consumer5.start(); consumer6.start(); } } class Food { } class FoodProducer implements Runnable { MyArrayBlockingQueue<Food> foodQueue; public FoodProducer(MyArrayBlockingQueue<Food> foodQueue) { super(); this.foodQueue = foodQueue; } @Override public void run() { while (true) { try { Thread.sleep((long) (Math.random() * 1000)); Food food = new Food(); foodQueue.enqueue(food); System.out.println(String.format( "Thread: [%s] produces food: [%s]", Thread.currentThread(), food)); } catch (InterruptedException e) { e.printStackTrace(); } } } } class FoodConsumer implements Runnable { MyArrayBlockingQueue<Food> foodQueue; public FoodConsumer(MyArrayBlockingQueue<Food> foodQueue) { super(); this.foodQueue = foodQueue; } @Override public void run() { while (true) { try { Thread.sleep(1000L); Food food = foodQueue.dequeue(); Thread.sleep(1000L); System.out.println(String.format( "Thread: [%s] consumes food: [%s]", Thread.currentThread(), food)); } catch (InterruptedException e) { e.printStackTrace(); } } } }
4) BlockingQueue provided by Java:
1> ArrayBlockingQueue
2> LinkedBlockingQueue
3> PriorityBlockingQueue
Operations: Will dig into the realization of these BlockingQueues.
Reference Links:
1) http://stackoverflow.com/questions/16760513/how-does-wait-get-the-lock-back-in-java
2) http://tutorials.jenkov.com/java-concurrency/blocking-queues.html
3) Core Java Volume II
相关推荐
1、JavaSE:Java入门 2、JavaSE:基础语法 3、JavaSE:流程控制 4、JavaSE:方法 5、JavaSE:数组 6、JavaSE:面向对象 7、JavaSE:异常机制 8、JavaSE:常用类 9、JavaSE:集合框架 10、JavaSE:IO流 11、JavaSE:...
1、JavaSE:Java入门 2、JavaSE:基础语法 3、JavaSE:流程控制 4、JavaSE:方法 5、JavaSE:数组 6、JavaSE:面向对象 7、JavaSE:异常机制 8、JavaSE:常用类 9、JavaSE:集合框架 10、JavaSE:IO流 11...
1、JavaSE:Java入门.pdf 2、JavaSE:基础语法.pdf 3、JavaSE:流程控制.pdf 4、JavaSE:方法.pdf 5、JavaSE:数组.pdf 6、JavaSE:面向对象.pdf 7、JavaSE:异常机制.pdf 8、JavaSE:常用类.pdf 9、JavaSE...
在你提到的"zxing-3.1.0.jar"和"zxing-javase-3.1.0.jar"这两个文件中,我们主要关注的是Java版本的ZXing库。 `zxing-3.1.0.jar` 是核心库,包含了ZXing的主要功能,如解码和编码各种条码格式。这个库可以独立使用...
32_文件断点上传器.avi 所在项目:videoUpload & javaSE应用:socket 33_为应用添加多个Activity与参数传递.avi 所在项目:MulActivity 34_Activity的启动模式.avi 所在项目:LaunchMode & openSingleInstance & ...
Java多线程编程是JavaSE(Standard Edition)核心技术之一,被广泛应用于复杂应用系统的设计中,以实现高效的并发处理。 首先,我们要理解什么是线程。线程,也可以理解为进程内部的执行路径,是程序中最小的执行...
这是一套超级详细的狂神说内容PDF笔记,从Java基础内容到微服务,分布式相关笔记,docker相关笔记等,包含有:JavaSE基础语法、Java入门、前端、网络编程、SpringBoot入门及技术、Vue精讲、Linux使用、JVM探究等方面...
32_文件断点上传器.avi 所在项目:videoUpload & javaSE应用:socket 33_为应用添加多个Activity与参数传递.avi 所在项目:MulActivity 34_Activity的启动模式.avi 所在项目:LaunchMode & openSingleInstance & ...
JavaSE中的数组是一种重要的数据结构,它允许程序员存储和管理一组具有相同数据类型的元素。数组的概念可以类比为一个容器,其中每个元素都有一个唯一的标识,即数组下标,用于区分不同的元素。数组的使用使得处理多...
在JavaSE的学习中,方法是构成程序的基本单元之一。方法可以定义为语句的集合,执行特定任务并提供代码复用。本文档首先解释了方法的概念,然后详细介绍了方法的定义、命名规则、调用以及重载等知识点。 首先,方法...
JavaSE实践五子棋和中国象棋游戏,棋盘,棋子绘制,输赢判定重置棋盘,单机博弈。 博客地址:https://blog.csdn.net/paroleg/article/details/139531741?spm=1001.2014.3001.5501
以上是根据给定文件信息中JavaSE网络编程部分的详细知识点。这些知识点不仅覆盖了网络编程的基础概念,还涉及了Java编程语言中关于网络编程的具体实现和应用,为深入学习Java网络编程打下坚实的理论基础。
JavaSE是Java标准版的缩写,涵盖Java编程语言的核心内容,包括基本语法、面向对象编程、异常处理、数据结构和算法等。 文档注释在Java中的重要性在于,它不仅仅是注释代码,更是在开发大型项目或库时,为生成API...
项目概述:获取用户输入的年、月,输出该月日历,需要确定1号是周几; 适用人群:初学Java的学习者,想要巩固自身循环、判断、方法等部分的学习者。
内容概要:新增图书、查看图书列表、删除、借阅、归还、图书排行榜、退出程序; 适用人群:Java初学者,想要用项目巩固自身知识掌握的学者
赠送jar包:javase-2.2.jar; 赠送原API文档:javase-2.2-javadoc.jar; 赠送源代码:javase-2.2-sources.jar; 赠送Maven依赖信息文件:javase-2.2.pom; 包含翻译后的API文档:javase-2.2-javadoc-API文档-中文...
JavaSE / ├─expert │ ├─anotation │ │ ├─AnnotationTest.java │ │ └─JavaDocTest.java │ ├─aop │ │ ├─Interceptor.java │ │ ├─Logger.java │ │ ├─LoginManage.java │ │ ├─...
JavaSE,全称为Java Standard Edition,是Java平台的标准版,主要面向桌面应用和服务器端开发。它是Java技术的基础,提供了核心的类库和API,包括集合框架、多线程、网络编程、I/O流、数据库连接(JDBC)、图形用户...
赠送jar包:javase-3.3.3.jar; 赠送原API文档:javase-3.3.3-javadoc.jar; 赠送源代码:javase-3.3.3-sources.jar; 赠送Maven依赖信息文件:javase-3.3.3.pom; 包含翻译后的API文档:javase-3.3.3-javadoc-API...