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实现生产者和消费者
生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费速度也不一样,同时还要实现生产者 ...
随机推荐
- HTML: 仿写一个财经类静态的网页
要求:仿写一个静态的网页,主要采用HTML+CSS+DIV的布局方式, 新建两个文件:demo.html.demo.css 图片素材:image.zip demo.html代码如下: <!doc ...
- 小白教你玩转php的闭包
php5.3有一个非常赞的新特性,那就是支持匿名函数(闭包).匿名函数可用于动态创建函数,并保存到一个变量中.举个栗子: $func = function(){ exit('Hello world!! ...
- SocketServer model_use
#!/usr/bin/env python #-*- coding:utf- -*- import SocketServer class MySocker(SocketServer.BaseReque ...
- Xib和storyboard对比
- jquery实现input输入框实时输入触发事件代码
<input id="productName" name="productName" class="wid10" type=" ...
- linux 命令进阶
1. ls –i -i, --inode 显示每个文件的inode 号 查看inode 可以用于 同一个classloader加载同名class时,是以先加载到的class为准, ...
- git提交
1.git pull 本地已经commit 2.git checkout master 3.git pull 4.git checkout - 5.git merge master 6.git pus ...
- 自动档车的P档和N档的区别
P档时,驻车齿轮在变速箱内被部分锁死,所以此时平地上不踩刹车,车辆有一定的驻车效果.N档就是空档,发动机与传动轴在变速器位置彻底分离,没有连接.因此P档适合停车和驻车,就是到地方了,要熄火时挂P档,拉 ...
- HashedWheelTimer
HashedWheelTimer 是根据 Hashed and Hierarchical Timing Wheels: Data Structuresfor the Efficient Impleme ...
- 关于prototype
之前听过课,可是这一块没怎么听懂,最近练了两个例子,又问了问小石同学,朦朦胧胧,感觉还是不太懂,记录点心得 最基本的例子 function Box(name,age){ this.name=name; ...