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实现生产者和消费者
生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费速度也不一样,同时还要实现生产者 ...
随机推荐
- iOS苹果开发者客服电话地址
苹果开发者客服电话地址:https://developer.apple.com/contact/phone.php 中国大陆地区客服电话:4006 701 855 中国香港地区客服电话:(852) 2 ...
- iOS:app直播---采集篇
[如何快速的开发一个完整的iOS直播app](采集篇) 转载自简书@袁峥Seemygo:http://www.jianshu.com/p/c71bfda055fa 前言 开发一款直播app,首先需要采 ...
- 读取XML文档结构并写入内容
1.在项目中新建XML文档结构.xsd文件,在其中添加相应的节点. 2.读取文档结构并写入内容 string initFileName = @"D:\Config.xml"; Da ...
- Bash中的shopt选项
Bash中的shopt选项 http://blog.chinaunix.net/uid-20587169-id-1919110.html shopt命令用于显示和设置shell中的行为选项,通过这些选 ...
- 感知开源的力量-APICloud Studio开源技术分享会
2014.9.15 中国领先的“云端一体”移动应用云服务提供商APICloud正式发布2015.9.15,APICloud上线一周年,迎来第一个生日这一天,APICloud 举办APICloud St ...
- 第六篇 SQL Server代理深入作业步骤工作流
本篇文章是SQL Server代理系列的第六篇,详细内容请参考原文. 正如这一系列的前几篇所述,SQL Server代理作业是由一系列的作业步骤组成,每个步骤由一个独立的类型去执行.每个作业步骤在技术 ...
- C#通过存储过程进行查询
一直不确定C#可以通过存储过程进行查询,今天才确定是可以的. 可以返回DataReader或者Dataset.
- Java定时任务Timer、TimerTask与ScheduledThreadPoolExecutor详解
定时任务就是在指定时间执行程序,或周期性执行计划任务.Java中实现定时任务的方法有很多,本文从从JDK自带的一些方法来实现定时任务的需求. 一.Timer和TimerTask Timer和Tim ...
- Java基础之集合框架——使用集合Vector<>挑选演员(TryVector)
控制台程序. public class Person implements Comparable<Person> { // Constructor public Person(String ...
- Java Socket常见异常处理 和 网络编程需要注意的问题
在java网络编程Socket通信中,通常会遇到以下异常情况: 第1个异常是 java.net.BindException:Address already in use: JVM_Bind. 该异常发 ...