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实现生产者和消费者
生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费速度也不一样,同时还要实现生产者 ...
随机推荐
- Insert BLOB && CLOB from PL/SQL and JDBC
For PL/SQL 1)Create Directory Where BLOB resides. create or replace directory temp as '/oradata2'; - ...
- 获取启动画面图片的string
支持 iPhone 以下. 支持 iPhone 及 iPad +(NSString*)getLaunchImageName { NSArray* images= @[@"LaunchImag ...
- ASP.NET IIS设置 Session时间
1.打开IIS需设置的网站主页 2.打开主页IIS--ASP项目,如下图: 3.设置 会话属性---超时 的值,如下图:
- android framework浅析_转
Android系统从底向上一共分了4层,每一层都把底层实现封装,并暴露调用接口给上一层. 1. Linux内核(Linux Kernel) 1)Android运行在linux kernel 2.6之上 ...
- [转载]windows任务管理器中的工作设置内存,内存专用工作集,提交大小详解
windows任务管理器中的工作设置内存,内存专用工作集,提交大小详解 http://shashanzhao.com/archives/832.html 虽然是中文字,但是理解起来还是很困难,什么叫工 ...
- NSTimer 线程操作
http://www.jianshu.com/p/0c050af6c5ee 2.NSTimer的创建与撤销必须在同一个线程操作.performSelector的创建与撤销必须在同一个线程操作.
- networkcommon v3开源消息框架资料
http://www.cnblogs.com/csdev/category/870400.html
- win7无线网连接了,但是图标显示未连接
第一步: 打开控制面板,找到“管理工具”->“计算机管理” 第二步: 在控制台左边栏,选择“设备管理器”,然后在右侧展开“网络适配器” 第三步: 在每一个网络设备上点鼠标右键,然后选择“卸载”. ...
- poj 题目分类(1)
poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...
- csuoj 1396: Erase Securely
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1396 1396: Erase Securely Time Limit: 1 Sec Memory ...