方式一:
/**
* 使用非阻塞队列PriorityQueue及wait/notify方法实现一个阻塞队列
*
*/
class MyBlockingQueue {
public final static int queueSize = 10;
public static final PriorityQueue<Integer> queue = new PriorityQueue();
}
class Producer extends Thread {
public void run() {
while(true) {
synchronized(MyBlockingQueue.queue) {
while(MyBlockingQueue.queue.size() == MyBlockingQueue.queueSize) {
try {
System.out.println("烤猪数量已满,快来吃吧:" + MyBlockingQueue.queue.size());
MyBlockingQueue.queue.wait();
} catch(InterruptedException e) {
e.printStackTrace();
notify();
}
}
MyBlockingQueue.queue.offer(1);
System.out.println("我烤了一头猪,烤猪数量:"+MyBlockingQueue.queue.size());
MyBlockingQueue.queue.notify();
}
}
}
}
class Consumer extends Thread {
public void run() {
while(true) {
synchronized(MyBlockingQueue.queue) {
while(MyBlockingQueue.queue.size() == 0) {
try {
System.out.println("没有烤乳猪了,赶快生产一个:" + MyBlockingQueue.queue.size());
MyBlockingQueue.queue.wait();
} catch(InterruptedException e) {
e.printStackTrace();
notify();
}
}
MyBlockingQueue.queue.poll();
System.out.println("吃掉了一头烤乳猪,当前烤乳猪数量:"+MyBlockingQueue.queue.size());
MyBlockingQueue.queue.notify();
}
}
}
}
方式二:
/**
* 使用阻塞队列ArrayBlockingQueue实现生产者消费者问题
*
*/
class MyBlockingQueue {
public final static int queueSize = 10;
public static ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue(queueSize);
}
class Producer extends Thread {
public void run() {
while(true) {
try {
MyBlockingQueue.queue.put(1);
System.out.println("生产了一头烤乳猪,当前乳猪数量:" + MyBlockingQueue.queue.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer extends Thread {
public void run() {
while(true) {
try {
MyBlockingQueue.queue.take();
System.out.println("吃了一头烤乳猪,当前乳猪数量:" + MyBlockingQueue.queue.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
-
Java实现阻塞队列的两种方式
2019-10-03 20:45:30方式一:/*** 使用非阻塞队列PriorityQueue及wait/notify方法实现一个阻塞队列**/class MyBlockingQueue { public final static int queueSize = 10; public static final PriorityQueue<Integer> queue = new...转载于:https://www.cnblogs.com/yuanfei1110111/p/10170783.html
-
java 手写阻塞队列_Java实现阻塞队列的两种方式
2020-12-21 12:01:12方式一:importjava.util..../***使用非阻塞队列PriorityQueue以及notify(),wait()机制实现一个阻塞队列*@authoruser**/publicclassMyBlockingQueue{publicfinalstaticintqueueSize=10;publicstaticPriorityQueue...方式一:import java.util.PriorityQueue;
/**
* 使用非阻塞队列PriorityQueue以及notify(),wait()机制实现一个阻塞队列
* @author user
*
*/
public class MyBlockingQueue {
public final static int queueSize = 10;
public static PriorityQueue queue = new PriorityQueue<>();
public static void main(String[] args) {
Product p = new Product();
Consumer c = new Consumer();
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
//生产者
class Product implements Runnable{
@Override
public void run() {
product();
}
public void product() {
while(true) {
synchronized(MyBlockingQueue.queue) {
while(MyBlockingQueue.queue.size() == MyBlockingQueue.queueSize){
try {
System.out.println("仓库已经放不下烤猪了,赶快来吃吧。烤猪数量:"+MyBlockingQueue.queueSize);
MyBlockingQueue.queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
notify();
}
}
MyBlockingQueue.queue.offer(1);
System.out.println("我烤了一头猪。烤猪数量:" + MyBlockingQueue.queue.size());
MyBlockingQueue.queue.notify();
System.out.println();
}
}
}
}
//消费者
class Consumer implements Runnable{
@Override
public void run() {
consumer();
}
private void consumer() {
while(true) {
synchronized(MyBlockingQueue.queue){
while(MyBlockingQueue.queue.size() == 0) {
try{
System.out.println("没有烤猪了,赶快生产一个。烤猪数量:"+MyBlockingQueue.queue.size());
MyBlockingQueue.queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
notify();
}
}
MyBlockingQueue.queue.poll();
System.out.println("吃掉了一头烤猪。烤猪数量:" + MyBlockingQueue.queue.size());
MyBlockingQueue.queue.notify();
}
}
}
}
方式二:import java.util.concurrent.ArrayBlockingQueue;
/**
* 使用阻塞队列ArrayBlockingQueue实现生产者消费者问题
* @author user
*
*/
public class MyBlockingQueue2 {
public final static int queueSize = 10;
public static ArrayBlockingQueue queue = new ArrayBlockingQueue<>(queueSize);
public static void main(String[] args) {
Product2 p = new Product2();
Consumer2 c = new Consumer2();
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
//生产者
class Product2 implements Runnable{
@Override
public void run() {
product();
}
public void product() {
while(true) {
try{
MyBlockingQueue2.queue.put(1);
System.out.println("生产了一头烤猪。烤猪数量:" + MyBlockingQueue2.queue.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//消费者
class Consumer2 implements Runnable{
@Override
public void run() {
consumer();
}
private void consumer() {
while(true) {
try{
MyBlockingQueue2.queue.take();
System.out.println("吃掉了一头烤猪。烤猪数量:" + MyBlockingQueue2.queue.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
-
Java可阻塞队列的两种实现方式
2015-09-14 18:21:01在Java中,对于Lock和Condition可以理解为对传统的synchronized和wait/notify机制的替代。 wait/notify有个限制,调用wait/notify的线程必须持有对象的锁。 This method should only be called by a thread ...在Java中,对于Lock和Condition可以理解为对传统的synchronized和wait/notify机制的替代。
wait/notify有个限制,调用wait/notify的线程必须持有对象的锁。
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
Throws:
IllegalMonitorStateException - if the current thread is not the owner of this object's monitor.
通常使用wait/notify的代码是这个样子的:
synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition }
在Condition接口的javadoc中,有一个经典的Condition例子,用Condition实现了一个可阻塞队列。这里仿照javadoc简单实现了一个可阻塞队列。为了简单,没有进行try/catch,同时加入了一些注释。
public class BoundedBuffer { final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondition(); final Condition notEmpty = lock.newCondition(); final Object[] items = new Object[2]; // 阻塞队列 int putptr, takeptr, count; public void put(Object x) throws InterruptedException { System.out.println("进入put"); lock.lock(); System.out.println("put lock 锁住"); try { while (count == items.length) { // 如果队列满了,notFull就一直等待 System.out.println("put notFull 等待"); notFull.await(); // 调用await的意思取反,及not notFull -> Full } items[putptr] = x; // 终于可以插入队列 if (++putptr == items.length) putptr = 0; // 如果下标到达数组边界,循环下标置为0 ++count; System.out.println("put notEmpty 唤醒"); notEmpty.signal(); // 唤醒notEmpty } finally { System.out.println("put lock 解锁"); lock.unlock(); } } public Object take() throws InterruptedException { lock.lock(); System.out.println("take lock 锁住"); try { while (count == 0) { System.out.println("take notEmpty 等待"); notEmpty.await(); } Object x = items[takeptr]; if (++takeptr == items.length) takeptr = 0; --count; System.out.println("take notFull 唤醒"); notFull.signal(); return x; } finally { lock.unlock(); System.out.println("take lock 解锁"); } } public static void main(String[] args) throws InterruptedException { final BoundedBuffer bb = new BoundedBuffer(); System.out.println(Thread.currentThread()+","+bb); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000); System.out.println(Thread.currentThread()+","+bb); bb.put("xx"); bb.put("yy"); bb.put("zz"); bb.put("zz"); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); bb.take(); } }
如果不使用JUC,大概是这样的:
public class BoundedBuffer_Synchronized { private Object[] items = new Object[2]; private Object notEmpty = new Object(); private Object notFull = new Object(); int count,putidx,takeidx; public void put(Object obj) throws InterruptedException{ synchronized(notFull){ while(count == items.length){ notFull.wait(); } } items[putidx] = obj; if(++putidx == items.length){ putidx = 0; } count ++; synchronized (notEmpty) { notEmpty.notify(); } } public Object take() throws InterruptedException{ synchronized(notEmpty){ while(count == 0){ // 啥也没有呢 取啥 notEmpty.wait(); } } Object x = items[takeidx]; System.out.println("取第"+takeidx+"个元素"+x); if(++takeidx == items.length){ takeidx = 0; } count --; synchronized (notFull) { notFull.notify(); } return x; } public static void main(String[] args) throws InterruptedException { final BoundedBuffer_Synchronized bb = new BoundedBuffer_Synchronized(); System.out.println(Thread.currentThread()+","+bb); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000); System.out.println(Thread.currentThread()+","+bb); bb.put("xx"); bb.put("yy"); bb.put("zz"); bb.put("zz"); bb.put("zz"); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); bb.take(); bb.take(); } }
从功能上来讲,两者实现了可阻塞队列的基本业务需求。Condition是配合Lock使用的,而wait/notify是配合synchronized使用的。比较两种实现方式,其实就是比较Lock和synchronized两种同步机制的区别。关于这方面,可以参考Java 理论与实践: JDK 5.0 中更灵活、更具可伸缩性的锁定机制
-
java队列 notify_Java可阻塞队列的两种实现方式 (传统wait/notify和jdk1.5以后的lock)
2021-02-28 07:16:29在Java中,对于Lock和Condition可以理解为对传统的synchronized和wait/notify机制的替代。wait/notify有个限制,调用wait/notify的线程必须持有对象的锁。This method should only be called by a thread that is ...在Java中,对于Lock和Condition可以理解为对传统的synchronized和wait/notify机制的替代。
wait/notify有个限制,调用wait/notify的线程必须持有对象的锁。
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
Throws:
IllegalMonitorStateException - if the current thread is not the owner of this object's monitor.
通常使用wait/notify的代码是这个样子的:
synchronized (obj) {
while ()
obj.wait();
... // Perform action appropriate to condition
}
在Condition接口的javadoc中,有一个经典的Condition例子,用Condition实现了一个可阻塞队列。这里仿照javadoc简单实现了一个可阻塞队列。为了简单,没有进行try/catch,同时加入了一些注释。
public class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
final Object[] items = new Object[2]; // 阻塞队列
int putptr, takeptr, count;
public void put(Object x) throws InterruptedException {
System.out.println("进入put");
lock.lock();
System.out.println("put lock 锁住");
try {
while (count == items.length) { // 如果队列满了,notFull就一直等待
System.out.println("put notFull 等待");
notFull.await(); // 调用await的意思取反,及not notFull -> Full
}
items[putptr] = x; // 终于可以插入队列
if (++putptr == items.length)
putptr = 0; // 如果下标到达数组边界,循环下标置为0
++count;
System.out.println("put notEmpty 唤醒");
notEmpty.signal(); // 唤醒notEmpty
} finally {
System.out.println("put lock 解锁");
lock.unlock();
}
}
public Object take() throws InterruptedException {
lock.lock();
System.out.println("take lock 锁住");
try {
while (count == 0) {
System.out.println("take notEmpty 等待");
notEmpty.await();
}
Object x = items[takeptr];
if (++takeptr == items.length)
takeptr = 0;
--count;
System.out.println("take notFull 唤醒");
notFull.signal();
return x;
} finally {
lock.unlock();
System.out.println("take lock 解锁");
}
}
public static void main(String[] args) throws InterruptedException {
final BoundedBuffer bb = new BoundedBuffer();
System.out.println(Thread.currentThread()+","+bb);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread()+","+bb);
bb.put("xx");
bb.put("yy");
bb.put("zz");
bb.put("zz");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
bb.take();
}
}
如果不使用JUC,大概是这样的:
public class BoundedBuffer_Synchronized {
private Object[] items = new Object[2];
private Object notEmpty = new Object();
private Object notFull = new Object();
int count,putidx,takeidx;
public void put(Object obj) throws InterruptedException{
synchronized(notFull){
while(count == items.length){
notFull.wait();
}
}
items[putidx] = obj;
if(++putidx == items.length){
putidx = 0;
}
count ++;
synchronized (notEmpty) {
notEmpty.notify();
}
}
public Object take() throws InterruptedException{
synchronized(notEmpty){
while(count == 0){ // 啥也没有呢 取啥
notEmpty.wait();
}
}
Object x = items[takeidx];
System.out.println("取第"+takeidx+"个元素"+x);
if(++takeidx == items.length){
takeidx = 0;
}
count --;
synchronized (notFull) {
notFull.notify();
}
return x;
}
public static void main(String[] args) throws InterruptedException {
final BoundedBuffer_Synchronized bb = new BoundedBuffer_Synchronized();
System.out.println(Thread.currentThread()+","+bb);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread()+","+bb);
bb.put("xx");
bb.put("yy");
bb.put("zz");
bb.put("zz");
bb.put("zz");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
bb.take();
bb.take();
}
}
从功能上来讲,两者实现了可阻塞队列的基本业务需求。Condition是配合Lock使用的,而wait/notify是配合synchronized使用的。比较两种实现方式,其实就是比较Lock和synchronized两种同步机制的区别。关于这方面,可以参考Java 理论与实践: JDK 5.0 中更灵活、更具可伸缩性的锁定机制
-
java 手写阻塞队列_Java阻塞队列的简单实现
2020-12-21 12:01:08来源:Java 并发常用的组件中有一种队列叫阻塞队列(BlockingQueue),当...本文会通过两种方式来实现简单的有界阻塞队列,在最后分别测试不同实现的性能差异。Monitor 和 Condition看过 Java 并发相关书籍的同学应该... -
Java可阻塞队列的两种实现方式 (传统wait/notify和jdk1.5以后的lock)
2015-03-10 23:29:00比较两种实现方式,其实就是比较Lock和synchronized两种同步机制的区别。关于这方面,可以参考 Java 理论与实践: JDK 5.0 中更灵活、更具可伸缩性的锁定机制 转载于:... -
Java阻塞队列的简单实现
2019-12-17 23:17:08来源: ...Java 并发常用的组件中有一种队列叫阻塞队列(BlockingQueue),当队列为空时,获取元素的线程会阻塞等待直到队列有数据;当队列满时,想要存储元素的线程会阻塞等待...本文会通过两种方式来实现简单的有界... -
JAVA中阻塞队列的类别和区别(转载)
2020-01-01 15:00:29这篇文章将介绍什么是阻塞队列,以及Java中阻塞队列的4种处理方式,并介绍Java 7中提供的7种阻塞队列,最后分析阻塞队列的一种实现方式。 阻塞队列(BlockingQueue)是一个支持两个附加操作的队列。这两个附加的... -
java阻塞队列与非阻塞队列
2019-10-04 11:55:59如果要实现一个线程安全的队列有两种方式:一种是使用阻塞算法,另一种是使用非阻塞算法。 //使用阻塞算法的队列可以用一个锁(入队和出队用同一把锁)或两个锁(入队和出队用不同的锁)等方式来实现。非阻塞的... -
Java中的阻塞队列
2019-09-20 12:36:53如果要实现一个线程安全的队列有两 种方式:一种是使用阻塞算法,另一种是使用非阻塞算法。使用阻塞算法的队列可以用一个锁 (入队和出队用同一把锁)或两个锁(入队和出队用不同的锁)等方式来实现。非阻塞的实现... -
java中的阻塞和非阻塞队列
2020-08-07 11:44:13实现一个队列的线程安全,有两种方式: 1)使用阻塞队列,即出队和入队共用一把锁或者各自使用一把锁来实现 2)非阻塞队列:可以利用循环CAS的方式实现 java中的阻塞队列 阻塞队列是一个支持两个附加操作的队列,... -
JAVA并发:阻塞队列
2020-11-26 23:31:27如果要实现一个线程安全的队列有两种方式:一种是使用阻塞算法,另一种是使用非阻塞算法。使用阻塞算法的队列可以用一个锁(入队和出队用同一把锁)或两个锁(入队和出队用不同的锁) 等方式来实现。非阻塞的实现方式则... -
并发:Java中的阻塞队列(BlockingQueue)。
2018-12-11 09:03:54本文将介绍什么是阻塞队列,以及Java中阻塞队列的4种处理方式,并介绍Java 7中提供的7种阻塞队列,最后分析阻塞队列的一种实现方式。 什么是阻塞队列 阻塞队列是一个支持两个附加操作的队列。这两个附加的操作... -
JAVA并发容器-阻塞队列
2019-11-28 09:58:28如果要实现一个线程安全的队列有两 种方式:一种是使用阻塞算法,另一种是使用非阻塞算法。使用阻塞算法的队列可以用一个锁 (入队和出队用同一把锁)或两个锁(入队和出队用不同的锁)等方式来实现。非阻塞的实现... -
java多线程-阻塞队列实现消费者生产者
2019-07-19 22:53:31注:这篇博文主要将使用阻塞队列实现,至于前面的两种可以看看我的另外一篇博客 二,什么是阻塞队列,阻塞队列的特性 1,一个支持两个附加操作的队列。这两个附加的操作支持阻塞的插入和移除方法。 2,Jav... -
Java中的阻塞队列-ConcurrentLinkedQueue
2019-04-17 19:03:00如果我们要实现一个线程安全的队列有两种实现方式一种是使用阻塞算法,另一种是使用非阻塞算法。使用阻塞算法的队列可以用一个锁(入队和出队用同一把锁)或两个锁(入队和出队用不同的锁)等方式来实现,而非阻塞的... -
Java并发编程之阻塞队列
2018-04-27 21:09:00实现线程安全的队伍有2种方式: 阻塞式的, 也就是加锁 非阻塞式的, 使用CAS, ConcurrentLinkedQueue就是使用的这种方式 阻塞队列提供两个附加的操作, 阻塞添加和阻塞移除: 阻塞添加: 当队列满时, 队列会阻塞添加... -
java JUC 并发包之阻塞队列——BlockQueue
2020-09-10 20:05:42在学习完java的同步队列、Lock和等待通知...jdk为我们提供了一下几种阻塞队列,他们的实现方式几乎是相同的,我们后面以一种方式讲述阻塞队列的实现,如下表为jdk为我们提供的几种阻塞队列与描述: 阻塞队列 描述 -
Java并发——非阻塞队列ConcurrentLinkedQueue
2018-07-29 14:15:08种方式:一种是使用阻塞算法,另一种是使用非阻塞算法。使用阻塞算法的队列可以用一个锁 (入队和出队用同一把锁)或两个锁(入队和出队用不同的锁)等方式来实现。非阻塞的实现方式则可以使用循环CAS的方式来实现。... -
Java并发——非阻塞队列之ConcurrentLinkedQueue源码解析
2020-06-27 18:47:49队列实现线程安全的方式有两种:非阻塞队列和阻塞队列。本篇文章我们先研究非阻塞队列。 非阻塞队列 在Java中要实现非阻塞的线程安全,一定绕不开自旋和CAS这一对好搭配。基于这个认知,我们来深入剖析一波Doug Lea... -
Java并发容器之非阻塞队列ConcurrentLinkedQueue
2018-10-19 17:00:31实现一个线程安全的队列有两种实现方式:一种是使用阻塞算法,阻塞队列就是通过使用加锁的阻塞算法实现的;另一种非阻塞的实现方式则可以使用循环CAS(比较并交换)的方式来实现。 ConcurrentLinkedQueue是一个... -
Java实现生产者 消费者模式的两种方式带源码
2019-10-29 21:47:521,使用阻塞队列blockingqueue实现简单的生产者消费者模型 原理:阻塞队列BlockingQueue本身就是线程安全,同时使用阻塞队列提供的take,put方法在操作阻塞队列会是使得队列进入阻塞。因此阻塞队列就是线程安全的。... -
Java 并发 --- 非阻塞队列之ConcurrentLinkedQueue源码分析
2017-12-19 19:48:53在并发编程中,有时候需要使用线程安全的队列,如果要实现一个线程安全的队列有两种方式:一种是使用阻塞算法,另一种是使用非阻塞算法,在前面我们逐一分析过阻塞队列,这篇文章过后,会写篇关于阻塞队列的总结,也... -
Java实现同步的几种方式
2016-11-14 00:19:32每个锁对象(JLS中叫monitor)都有两个队列,一个是就绪队列,一个是阻塞队列,就绪队列存储了将要获得锁的线程,阻塞队列存储了被阻塞的线程,当一个线程被唤醒(notify)后,才会进入到就绪队列,等待CPU的调度,反之...
-
自动化测试Python3+Selenium3+Unittest
-
MySQL 索引
-
重庆理工大学《数字逻辑电路》期末考试试卷.pdf
-
Mybtis-plus 自动生成代码
-
MySQL 多平台多模式(安装、配置和连接 详解)
-
华为1+X——网络系统建设与运维(中级)
-
JEDEC-DDR4-内存标准
-
libFuzzer视频教程
-
从洪堡精神到调优项目:欧盟主要国家的学士学位课程改革..pdf
-
React Native性能优化:应该做和不应该做的
-
重庆理工大学《概率论与数理统计》3套历年期末考试试卷(含答案).pdf
-
社交网络图中结点的“重要性”计算 (30 分)(Floyd)
-
2021-03-03
-
MySQL 主从复制 Replication 详解(Linux 和 W
-
selenium之WebDriverWait类
-
重庆理工大学《大学物理》习题册答案.pdf
-
Leetcode两数相加
-
vim的基本使用
-
MHA 高可用 MySQL 架构与 Altas 读写分离
-
MySQL 数据库的基本操作(数据完整性约束)