package pinx.thread;

 import java.util.LinkedList;
import java.util.Queue; public class ProducerConsumer { Storage storage=null;
Query query=null;
public ProducerConsumer(int max){
storage=new Storage(max);
query=new Query();
} public void start(){
Producter producter=new Producter();
Consumer consumer=new Consumer();
Thread thread1=new Thread(producter); Thread thread2=new Thread(consumer);
Thread thread3=new Thread(consumer);
Thread thread4=new Thread(consumer); thread1.start();
thread2.start();
thread3.start();
thread4.start();
} /**
* @param args
*/
public static void main(String[] args) {
ProducerConsumer pc=new ProducerConsumer(20);
pc.start();
} class Product {
private int id; public Product(int id) {
this.id = id;
} public String toString() {
return "Product " + this.id;
}
} class Query{
public Query(){}
public synchronized int get(){
return -1;
}
} class Storage {
Queue<Product> queue = null;
int max = 0; public Storage(int max) {
this.max = max;
this.queue = new LinkedList<Product>();
} public synchronized void push(Product product) {
while (this.max == this.queue.size()) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.queue.offer(product);
System.out.println("Producter has generated a product :"
+ product.toString());
this.notifyAll();
} public synchronized Product pop() {
while (this.queue.isEmpty()) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Product product = this.queue.poll();
System.out.println("Consumer has consumed a product :"
+ product.toString()+" ~~~");
return product;
}
} class Consumer implements Runnable { public void run() {
while(true){
System.out.println(String.format("Producter get the query value : %d", query.get()));
storage.pop();
try {
Thread.sleep((int)(Math.random()*1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} class Producter implements Runnable { public void run() {
int index=0;
while(true){
System.out.println(String.format("Producter get the query value : %d~~~~~~~~~~~~~~~~~~~", query.get()));
storage.push(new Product(++index));
try {
Thread.sleep((int)(Math.random()*1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} }

Producter and Consumer的更多相关文章

  1. Java Api Consumer 连接启用Kerberos认证的Kafka

    java程序连接到一个需要Kerberos认证的kafka集群上,消费生产者生产的信息,kafka版本是2.10-0.10.0.1: Java程序以maven构建,(怎么构建maven工程,可去问下度 ...

  2. 基础学习day12--多线程一线程之间的通信和常用方法

    一.线程之间的通信 1.1.线程之间的通信方法 多个线程在处理统一资源,但是任务却不同,这时候就需要线程间通信.    等待/唤醒机制涉及的方法:    1. wait():让线程处于冻结状态,被wa ...

  3. RocketMQ学习记录

    RocketMQ是一款分布式.队列模型的消息中间件,具有以下特点: 1.能够保证严格的消息顺序 2.提供丰富的消息拉取模式 3.高效的订阅者水平扩展能力 4.实时的消息订阅机制 5.亿级消息堆积能力 ...

  4. java学习之生产者和消费者案例

    package com.gh.thread; /** * 生产者和消费者案例 * wait和sleep的区别 * wait不让出监视器锁,sleep让出监视器的锁 * @author ganhang ...

  5. JAVA多线程经典问题 -- 生产者 消费者 同步队列实现方法

    在JAVASE5 中的java.util.concurrent.BlockingQueue支持,BlockingQueue是一个接口但是我们通常可以使用LinkedBlockingQueue,它是一个 ...

  6. python并行任务之生产消费模式

    一. 生产者/消费者模式 概念:生产者产生一块数据,放到buffer中,与此同时,消费者在从buffer中取出并消耗这些数据 理解:像生活中厂家生产出产品,顾客购买消耗这些产品,buffer就是存放商 ...

  7. java线程之生产者消费者

    看了毕向东老师的生产者消费者,就照着视频参考运行了一下,感觉还好 这个值得学习的是条理特别清晰: ProducterConsumerDemo.java中,一个资源类Resources,生产者消费者都可 ...

  8. java 生产者 与 消费者的案例

    主要理解了两个问题 1.线程数据同步的问题 2.线程交替运行的方式 package ThreadDemo; /** * 生产者与消费者的案例(一,同步的问题,值的问题 二,交替执行的问题) * @au ...

  9. java线程间通信:一个小Demo完全搞懂

    版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程系列文章只是自己知识的总结梳理,都是最基础的玩意,已经掌握熟练的可以绕过. 一.从一个小Demo说起 上篇我们聊到了Java多线程的同步 ...

随机推荐

  1. SDL2 gif动态图加载

    参照 https://tieba.baidu.com/p/3569073088?tpl=5&red_tag=1777318765 使用mingw工具链 #include <stdbool ...

  2. MySQL连接查询驱动表被驱动表以及性能优化

    准备我们需要的表结构和数据 两张表 studnet(学生)表和score(成绩)表, 创建表的SQL语句如下 CREATE TABLE `student` ( `id` int(11) NOT NUL ...

  3. MySQL 性能优化之慢查询

    性能优化的思路 首先需要使用慢查询功能,去获取所有查询时间比较长的SQL语句 其次使用explain命令去查询由问题的SQL的执行计划(脑补链接:点我直达1,点我直达2) 最后可以使用show pro ...

  4. 使用LaTeX输入矩阵

    当前各种文本编辑器支持的LaTeX数学公式库大多基于KaTeX,或者在Web中用MathJax的比较多,下面给出一种在Web中输入矩阵的例子 $$\left[ \begin{array}{cccc}a ...

  5. Oracle调用Java方法(下)复杂Jar包封装成Oracle方法以及ORA-29521错误

    上一篇随笔中已经说了简单的Jar是如何封装的,但是我的需求是根据TIPTOP的查询条件产生XML文件并上传到FTP主机中,那么就要涉及到XML生成的方法和FTP上传的方法 所以在Eclipse写的时候 ...

  6. Spring 中Controller 获取请求参数的方法笔记

    1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交.若"Content-Type"="application/ ...

  7. 微信小程序-超出部分显示省略号(单行与多行)

    /*单行*/ .work_font{ display:block /*这里设置inline-block或者block:根据使用情况来定(行内元素需要加这个,块级元素和行内块级可以不用)*/ white ...

  8. el-table表头与内容右边框错位问题

    在用element写一个后台管理系统时遇到一个坑,el-table标签渲染出数据后表头th和内容会有错位,之前没有的 修改大小都没用 于是看百度搜索一波最终解决修改之前 虽然不大 但是别扭 在全局添加 ...

  9. MarkDown编辑器的区别对比

    标题: MarkDown编辑器的区别对比 作者: 梦幻之心星 sky-seeker@qq.com 标签: [MarkDown, 编辑器,区别] 目录: [软件] 日期: 2020-6-22 前提说明 ...

  10. 玩转SpringBoot之捣鼓 Redis

    我们都知道,把首页数据放到Redis里,能够加快首页数据的访问速度.但是我们要如何准确又快速的将 Redis 整合到自己的 SpringBoot2.x 项目中呢?今天阿淼就带大家爬一爬其中的门门道道. ...