第一种方式 使用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的更多相关文章

  1. 基于Java 生产者消费者模式(详细分析)

    Java 生产者消费者模式详细分析 本文目录:1.等待.唤醒机制的原理2.Lock和Condition3.单生产者单消费者模式4.使用Lock和Condition实现单生产单消费模式5.多生产多消费模 ...

  2. Java生产者消费者的三种实现

    Java生产者消费者是最基础的线程同步问题,java岗面试中还是很容易遇到的,之前没写过多线程的代码,面试中被问到很尬啊,面完回来恶补下.在网上查到大概有5种生产者消费者的写法,分别如下. 用sync ...

  3. Java 生产者消费者模式详细分析

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  4. Java生产者消费者模型

    在Java中线程同步的经典案例,不同线程对同一个对象同时进行多线程操作,为了保持线程安全,数据结果要是我们期望的结果. 生产者-消费者模型可以很好的解释这个现象:对于公共数据data,初始值为0,多个 ...

  5. java 生产者消费者问题 并发问题的解决

    引言 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示,生产者向空间里存放数据,而消费者取用数据,如果不加以协调可能会出现以下情况: 生产者消费者图 ...

  6. JAVA生产者消费者的实现

    春节回了趟老家,又体验了一次流水席,由于桌席多,导致上菜慢,于是在等待间,总结了一下出菜流程的几个特点: 1.有多个灶台,多个灶台都在同时做菜出来. 2.做出来的菜,会有专人用一个托盘端出来,每次端出 ...

  7. java 生产者消费者问题 并发问题的解决(转)

    引言 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示,生产者向空间里存放数据,而消费者取用数据,如果不加以协调可能会出现以下情况: 生产者消费者图 ...

  8. 图文并茂的生产者消费者应用实例demo

    前面的几篇文章<<.NET 中的阻塞队列BlockingCollection的正确打开方式>><<项目开发中应用如何并发处理的一二事>>从代码以及理论角 ...

  9. Java生产者消费者问题

    1. package interview.thread; import java.util.LinkedList; import java.util.Queue; import org.apache. ...

随机推荐

  1. OrCAD原理图中怎么导出FPGA的引脚分配

    流程 (1)选择tool下的export FPGA: (2)选择厂商,选择器件型号.选择生成文件类型. 以上.

  2. 「POI2011 R2 Day2」Tree Rotations【线段树合并】

    题目链接 [BZOJ] [洛谷] [LOJ] 题解 由于是前序遍历,那么讨论一棵树上的逆序对的情况. 两个节点都在左子树上 两个节点都在右子树上 两个节点分别在不同的子树上. 前两种情况其实也可以归结 ...

  3. CentOS7防火墙问题

    CentOS6关闭防火墙使用以下命令, //临时关闭service iptables stop//禁止开机启动chkconfig iptables off CentOS7中若使用同样的命令会报错, s ...

  4. 内核空间内存申请函数kmalloc kzalloc vmalloc的区别

    我们都知道在用户空间动态申请内存用的函数是 malloc(),这个函数在各种操作系统上的使用是一致的,对应的用户空间内存释放函数是 free().注意:动态申请的内存使用完后必须要释放,否则会造成内存 ...

  5. pandas的读写

    import as pd import numpy as np import matplotlib.pyplot as plt #df.to_excel('C:Users/history/Deskto ...

  6. DML、DDL、DCL的分别是什么

    DML.DDL.DCL的分别是什么 一直以来,分不清这三者的简称代表什么,甚至在面试中遇到可能会张冠李戴.今天特意记录一下. 一.DML(data manipulation language) 数据操 ...

  7. 使用Docker构建nginx容器,并且启动后不会自动退出

    为什么docker运行后就自动退出? docker 容器默认会把容器内部第一个进程,也就是pid=1的程序作为docker容器是否正在运行的依据,如果docker 容器pid挂了,那么docker容器 ...

  8. netty的简单的应用例子

    一.简单的聊天室程序 public class ChatClient { public static void main(String[] args) throws InterruptedExcept ...

  9. python3 两层dict字典转置

    python3; 两层字典 dict =(type, dict2) dict2 = (k_value, index) dictss = { 10: {3: 1, 4: 2, 5: 3, 6: 4, 7 ...

  10. 1.7分布式工具配置及安装(仅供学习Xshell,VMware)

    前言 最近因为换工作以及其他的一些琐事,耽误了更博时间,再加上分布式的这几个软件之前没撸过....这学习这几个工具上也花了点时间 本篇博客为后续分布式的学习提供基础的安装和配置. 首先,系统为Cent ...