Producter and Consumer
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的更多相关文章
- Java Api Consumer 连接启用Kerberos认证的Kafka
java程序连接到一个需要Kerberos认证的kafka集群上,消费生产者生产的信息,kafka版本是2.10-0.10.0.1: Java程序以maven构建,(怎么构建maven工程,可去问下度 ...
- 基础学习day12--多线程一线程之间的通信和常用方法
一.线程之间的通信 1.1.线程之间的通信方法 多个线程在处理统一资源,但是任务却不同,这时候就需要线程间通信. 等待/唤醒机制涉及的方法: 1. wait():让线程处于冻结状态,被wa ...
- RocketMQ学习记录
RocketMQ是一款分布式.队列模型的消息中间件,具有以下特点: 1.能够保证严格的消息顺序 2.提供丰富的消息拉取模式 3.高效的订阅者水平扩展能力 4.实时的消息订阅机制 5.亿级消息堆积能力 ...
- java学习之生产者和消费者案例
package com.gh.thread; /** * 生产者和消费者案例 * wait和sleep的区别 * wait不让出监视器锁,sleep让出监视器的锁 * @author ganhang ...
- JAVA多线程经典问题 -- 生产者 消费者 同步队列实现方法
在JAVASE5 中的java.util.concurrent.BlockingQueue支持,BlockingQueue是一个接口但是我们通常可以使用LinkedBlockingQueue,它是一个 ...
- python并行任务之生产消费模式
一. 生产者/消费者模式 概念:生产者产生一块数据,放到buffer中,与此同时,消费者在从buffer中取出并消耗这些数据 理解:像生活中厂家生产出产品,顾客购买消耗这些产品,buffer就是存放商 ...
- java线程之生产者消费者
看了毕向东老师的生产者消费者,就照着视频参考运行了一下,感觉还好 这个值得学习的是条理特别清晰: ProducterConsumerDemo.java中,一个资源类Resources,生产者消费者都可 ...
- java 生产者 与 消费者的案例
主要理解了两个问题 1.线程数据同步的问题 2.线程交替运行的方式 package ThreadDemo; /** * 生产者与消费者的案例(一,同步的问题,值的问题 二,交替执行的问题) * @au ...
- java线程间通信:一个小Demo完全搞懂
版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程系列文章只是自己知识的总结梳理,都是最基础的玩意,已经掌握熟练的可以绕过. 一.从一个小Demo说起 上篇我们聊到了Java多线程的同步 ...
随机推荐
- 小师妹学JVM之:JIT中的LogCompilation
目录 简介 LogCompilation简介 LogCompilation的使用 解析LogCompilation文件 总结 简介 我们知道在JVM中为了加快编译速度,引入了JIT即时编译的功能.那么 ...
- 本地代码提交到远程仓库(git)
[准备环境] 我没有在Linux搭建gitlab私有云服务器,用的是开源的 gitee托管平台 1.在gitee注册账号 2.本地下载git客户端 [步骤] 1 本地新建1个文件夹 进入文件夹后 ...
- WeChair项目Beta冲刺(2/10)
团队项目进行情况 1.昨日进展 Beta冲刺第二天 昨日进展: 昨天由于组内成员课程繁重,但是大家还是花时间一起开会谈论了开发的一些细节和交流了一些问题 2.今日安排 前端:扫码占座功能和预约功 ...
- springboot的jar为何能独立运行
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 基于opencv的车牌提取项目
初学图像处理,做了一个车牌提取项目,本博客仅仅是为了记录一下学习过程,该项目只具备初级功能,还有待改善 第一部分:车牌倾斜矫正 # 导入所需模块 import cv2 import math from ...
- docker推送镜像到私有仓库
配置私有仓库源 私有仓库地址:registry.supos.ai 修改/etc/docker/daemon.json文件,增加insecure-registries,如下所示: { "ins ...
- 【总结】Asp.Net Mvc 后台控制器获取页面发来的参数类型
接收各种参数(普通参数,对象,JSON, URL) 待续...
- MySQL授权--WITH GRANT OPTION
今天在学习MySQL的时候,看到一句描述 WITH GRANT OPTION should be left out if the user need not be able to grant othe ...
- Spring笔记(3) - debug源码AOP原理解析
案例 @EnableAspectJAutoProxy//开启基于注解的aop模式 @Configuration public class AOPConfig { //业务逻辑类加入容器中 @Bean ...
- Java程序员阅读源码的小技巧,原来大牛都是这样读的,赶紧看看!
今天介跟大家分享一下我平时阅读源码的几个小技巧,对于阅读java中间件如Spring.Dubbo等框架源码的同学有一定帮助. 本文基于Eclipse IDE,我们每天都使用的IDE其实提供了很多强大的 ...