Java语言synchronized与lock实现生产者消费者
来源:互联网 发布:c语言memset 编辑:程序博客网 时间:2024/06/08 14:09
synchronized方式
package test;import java.util.LinkedList;import java.util.Queue;import java.util.Random;public class ProducerConsumer { public static void main(String args[]) { Queue<Integer> buffer = new LinkedList<>(); int maxSize = 10; ProducerConsumer producerConsumer = new ProducerConsumer(); Thread producer = producerConsumer.new Producer(buffer, maxSize, "PRODUCER"); Thread consumer = producerConsumer.new Consumer(buffer, maxSize, "CONSUMER"); producer.start(); consumer.start(); } class Producer extends Thread{ private Queue<Integer> queue; private int maxSize; public Producer(Queue<Integer> queue,int maxSize,String name) { super(name); this.queue = queue; this.maxSize = maxSize; } @Override public void run() { while(true) { synchronized (queue) { while(queue.size() == maxSize) { System.out.println("Queue is full"); try { queue.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Random random = new Random(); int i = random.nextInt(); System.out.println("Producing value : " + i); queue.add(i); queue.notifyAll(); } } } } class Consumer extends Thread{ private Queue<Integer> queue; private int maxSize; public Consumer(Queue<Integer> queue, int maxSize, String name){ super(name); this.queue = queue; this.maxSize = maxSize; } @Override public void run() { while (true) { synchronized (queue) { while (queue.isEmpty()) { System.out.println("Queue is empty"); try { queue.wait(); } catch (Exception ex) { ex.printStackTrace(); } } System.out.println("Consuming value : " + queue.remove()); queue.notifyAll(); } } } }}
lock方式
package test;import java.util.LinkedList;import java.util.Queue;import java.util.Random;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class LockProducerConsumer { Lock lock = new ReentrantLock(); Condition conditionP = lock.newCondition(); Condition conditionC = lock.newCondition(); public static void main(String args[]) { Queue<Integer> buffer = new LinkedList<>(); int maxSize = 10; LockProducerConsumer producerConsumer = new LockProducerConsumer(); Thread producer = producerConsumer.new Producer(buffer, maxSize, "PRODUCER"); Thread consumer = producerConsumer.new Consumer(buffer, "CONSUMER"); producer.start(); consumer.start(); } class Producer extends Thread{ private Queue<Integer> queue; private int maxSize; public Producer(Queue<Integer> queue,int maxSize,String name) { super(name); this.queue = queue; this.maxSize = maxSize; } @Override public void run() { while(true) { lock.lock(); try { while (true) { while (queue.size() == maxSize) { System.out.println("Queue is full"); try { conditionP.await(); } catch (InterruptedException e) { e.printStackTrace(); } } Random random = new Random(); int i = random.nextInt(); System.out.println("Producing value : " + i); queue.add(i); conditionC.signal(); } }finally { lock.unlock(); } } } } class Consumer extends Thread{ private Queue<Integer> queue; public Consumer(Queue<Integer> queue, String name){ super(name); this.queue = queue; } @Override public void run() { lock.lock(); try { while (true) { while (queue.isEmpty()) { System.out.println("Queue is empty"); try { conditionC.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Consuming value : " + queue.remove()); conditionP.signal(); } }finally { lock.unlock(); } } }}
阅读全文
0 0
- Java语言synchronized与lock实现生产者消费者
- synchronized与Lock 的区别&分别实现生产者/消费者问题
- JAVA多线程(五)用lock、synchronized、阻塞队列三种方法实现生产者消费者模式
- 使用 Lock 与Condition 实现生产者消费者
- Java实现生产者与消费者(四)(多生产者与多消费者使用lock同步锁)
- Java 实现生产者与消费者
- Java实现生产者与消费者
- java实现生产者与消费者
- java实现生产者与消费者
- java实现生产者与消费者
- synchronized 实现生产者消费者问题
- 生产者与消费者C语言,Java语言
- java多线程之多生产者与多消费者案例之Lock和Condition实现方式
- C语言生产者与消费者-随机数实现
- 生产者与消费者-《Java编程语言》
- 生产者与消费者问题Java实现
- 生产者消费者问题理解与Java实现
- 生产者消费者问题理解与Java实现
- 练习 20:文件系统:修改和创建文件系统,tune2fs,mkfs
- GAN眼中的图像翻译(附神奇歌单)
- Python3 内置函数
- Windows下Mysql的安装与配置
- python 以概率生成值
- Java语言synchronized与lock实现生产者消费者
- Ubuntu 更换软件源
- 仿QQ列表ExpandableListView详细写法(通过Java bean封装数据)
- dp问题中记录路径的方法
- 合并果子解题报告
- linux常用命令总结
- 实例说明optimize table在优化mysql时很重要
- VC++6.0中添加批量注释和取消批量注释快捷键
- Data structure of the experimental order of a: a row of fast row(learning quick sorting).