java 生产者消费者简单实现demo
第一种方式 使用BlockingQueue 阻塞队列
public class Threads {
public static void main(String[] args) {
final ArrayBlockingQueue<Integer> queue=new ArrayBlockingQueue<>(10);
produce produce=new produce(queue);
consumer consumer=new consumer(queue);
consumer.start();
produce.start();
}
static class produce extends Thread{
final ArrayBlockingQueue<Integer> integerArrayBlockingQueue;
public produce(ArrayBlockingQueue<Integer> integerArrayBlockingQueue) {
this.integerArrayBlockingQueue = integerArrayBlockingQueue;
}
@Override
public void run() {
while (true){
Integer random=new Random().nextInt(10);
integerArrayBlockingQueue.add(random);
System.out.println("shen chan shu ju"+random);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class consumer extends Thread{
final ArrayBlockingQueue<Integer> integerArrayBlockingQueue;
public consumer(ArrayBlockingQueue<Integer> integerArrayBlockingQueue) {
this.integerArrayBlockingQueue = integerArrayBlockingQueue;
}
@Override
public void run() {
while (true){
try {
Integer element=integerArrayBlockingQueue.take();
System.out.println("xiao fei shu ju "+element);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
第二种方法利用 wait()和 notifyAll()
public class Threads {
private static String lock = "lock";
public static void main(String[] args) {
final List<Integer> list = new ArrayList<>(10);
final Integer max = 10;
produce produce1 = new produce(list, max);
produce produce2 = new produce(list, max);
consumer consumer = new consumer(list, max);
consumer.start();
produce1.start();
produce2.start();
}
static class produce extends Thread {
final List<Integer> list;
final Integer max;
public produce(List<Integer> list, Integer max) {
this.list = list;
this.max = max;
}
@Override
public void run() {
while (true) {
synchronized (lock) {
while (list.size() > max) {
try {
lock.wait();
System.out.println("生产数据已满 线程" + Thread.currentThread().getName() + "已停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int random = new Random().nextInt(10);
list.add(random);
lock.notifyAll();
System.out.println("线程" + Thread.currentThread().getName() + "正在生产数据" + random);
}
}
}
}
static class consumer extends Thread {
final List<Integer> list;
final Integer max;
public consumer(List<Integer> list, Integer max) {
this.list = list;
this.max = max;
}
@Override
public void run() {
while (true) {
synchronized (lock) {
while (list.isEmpty()) {
try {
lock.wait();
System.out.println("消费数据已空 线程" + Thread.currentThread().getName() + "已停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int random = list.remove(0);
lock.notifyAll();
System.out.println("线程" + Thread.currentThread().getName() + "正在消费数据" + random);
}
}
}
}
}
第三种方法 ReentrantLock await() 和 signal() 实现
public class Threads {
private Lock lock=new ReentrantLock();
final Condition notfull=lock.newCondition();
final Condition notempty=lock.newCondition();
public static void main(String[] args) {
final List<Integer> list = new ArrayList<>(10);
final Integer max = 10;
Threads threads = new Threads();
produce produce1 = threads.new produce(list, max);
produce produce2 = threads.new produce(list, max);
consumer consumer = threads.new consumer(list, max);
consumer.start();
produce1.start();
produce2.start();
}
class produce extends Thread {
final List<Integer> list;
final Integer max;
public produce(List<Integer> list, Integer max) {
this.list = list;
this.max = max;
}
@Override
public void run() {
while (true) {
lock.lock();
while (list.size() > max) {
try {
notfull.await();
System.out.println("生产数据已满 线程" + Thread.currentThread().getName() + "已停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int random = new Random().nextInt(10);
list.add(random);
notempty.signal();
System.out.println("线程" + Thread.currentThread().getName() + "正在生产数据" + random);
}
}
}
class consumer extends Thread {
final List<Integer> list;
final Integer max;
public consumer(List<Integer> list, Integer max) {
this.list = list;
this.max = max;
}
@Override
public void run() {
while (true) {
lock.lock();
while (list.isEmpty()) {
try {
notempty.await();
System.out.println("消费数据已空 线程" + Thread.currentThread().getName() + "已停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int random = list.remove(0);
notfull.signal();
System.out.println("线程" + Thread.currentThread().getName() + "正在消费数据" + random);
}
}
}
}
java 生产者消费者简单实现demo的更多相关文章
- 基于Java 生产者消费者模式(详细分析)
Java 生产者消费者模式详细分析 本文目录:1.等待.唤醒机制的原理2.Lock和Condition3.单生产者单消费者模式4.使用Lock和Condition实现单生产单消费模式5.多生产多消费模 ...
- Java生产者消费者的三种实现
Java生产者消费者是最基础的线程同步问题,java岗面试中还是很容易遇到的,之前没写过多线程的代码,面试中被问到很尬啊,面完回来恶补下.在网上查到大概有5种生产者消费者的写法,分别如下. 用sync ...
- Java 生产者消费者模式详细分析
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- Java生产者消费者模型
在Java中线程同步的经典案例,不同线程对同一个对象同时进行多线程操作,为了保持线程安全,数据结果要是我们期望的结果. 生产者-消费者模型可以很好的解释这个现象:对于公共数据data,初始值为0,多个 ...
- java 生产者消费者问题 并发问题的解决
引言 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示,生产者向空间里存放数据,而消费者取用数据,如果不加以协调可能会出现以下情况: 生产者消费者图 ...
- JAVA生产者消费者的实现
春节回了趟老家,又体验了一次流水席,由于桌席多,导致上菜慢,于是在等待间,总结了一下出菜流程的几个特点: 1.有多个灶台,多个灶台都在同时做菜出来. 2.做出来的菜,会有专人用一个托盘端出来,每次端出 ...
- java 生产者消费者问题 并发问题的解决(转)
引言 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示,生产者向空间里存放数据,而消费者取用数据,如果不加以协调可能会出现以下情况: 生产者消费者图 ...
- 图文并茂的生产者消费者应用实例demo
前面的几篇文章<<.NET 中的阻塞队列BlockingCollection的正确打开方式>><<项目开发中应用如何并发处理的一二事>>从代码以及理论角 ...
- Java生产者消费者问题
1. package interview.thread; import java.util.LinkedList; import java.util.Queue; import org.apache. ...
随机推荐
- MRP没生成MRP汇总表
设置:工作日历延长
- Quill 富文本编辑器
Quill 富文本编辑器 https://quilljs.com/ https://github.com/quilljs/quill https://github.com/quilljs/awesom ...
- Linux学习之路4——文件IO打开、创建、读写操作
1.使用man 2 open.man 2 creat.man 2 write.man 2 read命令获取头文件 语法: int open(const char *pathname, int flag ...
- Azure DevOps
Azure DevOps https://azure.microsoft.com/zh-cn/services/devops/ It looks great!
- git版本回退
场景1: 当你改乱了工作区某个文件的内容,修改后未执行git add和git commit,想直接丢弃工作区的修改时,用命令git checkout -- file. 场景2: 当你不但改乱了工作区某 ...
- Xilinx FPGA 移位寄存器IP延时问题
软件版本:Vivado2016.1 在使用移位寄存器IP时,对于不同延时拍数的使能延时可能会有问题. (1)32深度的可变长度移位寄存器,IP生成界面如下图所示. (2)128深度的可变长度移位寄存器 ...
- SAM求多个串的最长公共子串
又学到一个\(SAM\)的新套路QvQ 思路 考虑用其中的一个串建个\(SAM\),然后用其他的串在上面匹配,匹配时更新答案 首先有一个全局变量\(len\),表示当前已匹配的长度.假设目前在点\(u ...
- 洛谷P2604 网络扩容 拆点+费用流
原题链接 这题貌似比较水吧,最简单的拆点,直接上代码了. #include <bits/stdc++.h> using namespace std; #define N 1000 #def ...
- django 2.接口之工作原理
1.创建应用程序有两种方法,第一种就是在新建项目的时候,在最初使的时填入应用程序名称,第二种就是进入目录下面,输入 python manage.py startapp appName 就会自动生成一个 ...
- ajax+json
ajax学习: 1.ajax的概念 局部刷新技术.不是一门新技术,是多种技术的组合,是浏览器端的技术 2 为什么要使用ajax? 传统的模式 是 发送请求 到 服务器 ,服务器经过 ...