Java生产者和消费者问题
容器类Box.java
public class Box {
private int num = 0;
public void put(){
if(num==10){
try {
System.out.println("生产者被阻塞");
this.wait(); } catch (InterruptedException e) {
e.printStackTrace();
}
return;
}
num++;
System.out.println("生产了一个,现在有"+getNum()+"个");
this.notify();
} public void get(){
if(num==0){
try {
System.out.println("消费者被阻塞");
this.wait();
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return;
}
num--;
System.out.println("消费了一个,现在有"+getNum()+"个");
this.notify();
} public int getNum(){
return num;
}
}
生产者Producer.java
public class Producer implements Runnable{
public Box box;
public int name; public Producer(Box box,int name){
this.box = box;
this.name = name;
} public void run() {
// TODO 自动生成方法存根
while(true){
try{
Thread.sleep(2000);
synchronized(box){
box.put();
} }catch(Exception e){
e.printStackTrace();
}
} }
} 消费者Consumer.java
public class Consumer implements Runnable{
public Box box;
public int name; public Consumer(Box box,int name){
this.box = box;
this.name = name;
} public void run() {
while(true){
try{
Thread.sleep(3000);
synchronized(box){
box.get();
}
}catch(Exception e){
e.printStackTrace();
}
} }
}
运行测试类Main.java
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Box b = new Box();
Producer p1 = new Producer(b,1);
Producer p2 = new Producer(b,2);
Producer p3 = new Producer(b,3); Consumer c1 = new Consumer(b,1);
Consumer c2 = new Consumer(b,2);
Consumer c3 = new Consumer(b,3); new Thread(p1).start();
new Thread(p2).start();
new Thread(p3).start();
new Thread(c1).start();
new Thread(c2).start();
new Thread(c3).start();
}
}容器类Box.java
public class Box {
private int num = 0;
public void put(){
if(num==10){
try {
System.out.println("生产者被阻塞");
this.wait(); } catch (InterruptedException e) {
e.printStackTrace();
}
return;
}
num++;
System.out.println("生产了一个,现在有"+getNum()+"个");
this.notify();
} public void get(){
if(num==0){
try {
System.out.println("消费者被阻塞");
this.wait();
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return;
}
num--;
System.out.println("消费了一个,现在有"+getNum()+"个");
this.notify();
} public int getNum(){
return num;
}
}
生产者Producer.java
public class Producer implements Runnable{
public Box box;
public int name; public Producer(Box box,int name){
this.box = box;
this.name = name;
} public void run() {
// TODO 自动生成方法存根
while(true){
try{
Thread.sleep(2000);
synchronized(box){
box.put();
} }catch(Exception e){
e.printStackTrace();
}
} }
} 消费者Consumer.java
public class Consumer implements Runnable{
public Box box;
public int name; public Consumer(Box box,int name){
this.box = box;
this.name = name;
} public void run() {
while(true){
try{
Thread.sleep(3000);
synchronized(box){
box.get();
}
}catch(Exception e){
e.printStackTrace();
}
} }
}
运行测试类Main.java
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Box b = new Box();
Producer p1 = new Producer(b,1);
Producer p2 = new Producer(b,2);
Producer p3 = new Producer(b,3); Consumer c1 = new Consumer(b,1);
Consumer c2 = new Consumer(b,2);
Consumer c3 = new Consumer(b,3); new Thread(p1).start();
new Thread(p2).start();
new Thread(p3).start();
new Thread(c1).start();
new Thread(c2).start();
new Thread(c3).start();
}
}
原文地址:http://user.qzone.qq.com/372806800/blog/1336197812
Java生产者和消费者问题的更多相关文章
- Java生产者与消费者(下)
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 上一讲我们让消费者和生产者都各停1毫秒,实际上大多并不是这样的.第二讲,我们讲一个极端的例子和一个正 ...
- Java生产者与消费者(上)
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 生产与消费者模式,是编程中最常用的模式之一,在多线程中应用比较明显.个人理解:在自助餐厅,厨师在不断 ...
- java生产者与消费者模式
前言: 生产者和消费者模式是我们在学习多线程中很经典的一个模式,它主要分为生产者和消费者,分别是两个线程, 目录 一:生产者和消费者模式简介 二:生产者和消费者模式的实现 声明:本例来源于java经典 ...
- java 生产者 与 消费者的案例
主要理解了两个问题 1.线程数据同步的问题 2.线程交替运行的方式 package ThreadDemo; /** * 生产者与消费者的案例(一,同步的问题,值的问题 二,交替执行的问题) * @au ...
- Java 生产者模式 消费者模式
// The standard idiom for calling the wait synchronized(sharedObject) { while(condition){ sharedObje ...
- java生产者,消费者
有很多实现的方法 使用blockingqueue实现 demo import java.util.concurrent.LinkedBlockingQueue; /** * Created by 58 ...
- JAVA并发框架之Semaphore实现生产者与消费者模型
分类: Java技术 锁和信号量(Semaphore)是实现多线程同步的两种常用的手段.信号量需要初始化一个许可值,许可值可以大于0,也可以小于0,也可以等于0. 如果大于0,表示 ...
- java 生产者消费者问题 并发问题的解决
引言 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示,生产者向空间里存放数据,而消费者取用数据,如果不加以协调可能会出现以下情况: 生产者消费者图 ...
- Java实现生产者和消费者
生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费速度也不一样,同时还要实现生产者 ...
随机推荐
- POJ 1035题目描述
Description You, as a member of a development team for a new spell checking program, are to write a ...
- appcon 图标打包
ERROR ITMS-90022: "Missing required icon file. The bundle does not contain an app icon for iPho ...
- python中的yield
在理解yield之前,要首先明白什么是generator,在理解generator之前首先要理解可迭代的概念. 可迭代(iterables)在你创建一个list的时候,可以逐个读取其中的元素,该逐个读 ...
- 伪类写border, transform: scale3d() 及兼容性
.top::before { content: ""; position: absolute; left: 0; width: 200%; height: 0; border-to ...
- Request、Servlet及其子接口
最近看tomcat源码,这类接口多的有点眩,整理出来看一下.(基于tomcat4) javax.servlet.ServletRequset接口,和org.apache.catalina.Reques ...
- 学习OpenCV——配置CUDA环境
大家都把GPU&CUDA说的很NB狠NB,于是,下一步想通过GPU加速程序运行.这一个星期,都在配置OpenCV的CUDA环境,今天终于以失败告终,原因是实验室的机器显卡不支持CUDA...伤 ...
- PostgreSQL9.1 upgrade to PostgreSQL9.5rc1
PostgreSQL9.1.0 upgrade to PostgreSQL9.5rc1 安装PG9.1端口为5432 [pgup@minion1 pg]$ ls postgresql-9.1.0.ta ...
- PostgreSQL Replication之第九章 与pgpool一起工作(7)
9.7 处理故障转移和高可用 可以使用pgpool来解决的一些明显的问题是高可用性和故障转移.一般来讲,有使用pgpool或者不使用pgpool可以用来处理这些问题的各种方法. 9.7.1 使用Pos ...
- 源码安装nginx以及平滑升级
源码安装nginx以及平滑升级 ...
- Codeforce Round #218 Div2
A:没个元素的个数少的变成多的和就是了 B:居然被systemtest搓掉了- -分东西,我改的代码,还是shit一样的过的...别人的直接两个操作数相减就可以了! C:二分题- -,没想到比赛时因为 ...