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实现生产者和消费者
生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费速度也不一样,同时还要实现生产者 ...
随机推荐
- androidSDK配置环境变量
android的开发人员来说,首先要做的就是环境变量的配置.java是需要配置环境变量的.当然,安卓的环境变量需要我们配置adb的使用,将开发平台的两个工具包配置到环境变量里. 工具/原料 and ...
- vb和php 基于socket通信
php代码(页面代码非cmd命令脚本) <?php $server = '127.0.0.1'; $port = 8888; $socket = socket_create(AF_INET, S ...
- Java开发遇到的问题及解决方案
一.java.lang.OutOfMemoryError 问题:myeclipse 内存不足,又显示内存溢出等问题怎么回事?( java.lang.OutOfMemoryError: PermGen ...
- Spring Boot 1 创建Demo
Spring Boot的主要优点: 为所有Spring开发者更快的入门 开箱即用,提供各种默认配置来简化项目配置 内嵌式容器简化Web项目 没有冗余代码生成和XML配置的要求 入门操作: 1.打开ht ...
- iOS网络协议 HTTP/TCP/IP浅析
一.TCP/IP协议 话说两台电脑要通讯就必须遵守共同的规则,就好比两个人要沟通就必须使用共同的语言一样.一个只懂英语的人,和一个只懂中文的人由于没有共同的语言(规则)就没办法沟通.两台电 ...
- CSS的叠加
CSS中的叠加分为以下三种: 1.上下叠加 CSS部分: #div1{ width:200px; height:50px; margin-bottom:30px; background:#ffff00 ...
- 2-sat按照最小字典序输出可行解(hdu1814)
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- fzuoj Problem 2179 chriswho
http://acm.fzu.edu.cn/problem.php?pid=2179 Problem 2179 chriswho Accept: 57 Submit: 136 Time Limi ...
- winform 控件开发1——复合控件
哈哈是不是丑死了? 做了一个不停变色的按钮,可以通过勾选checkbox停下来,代码如下: 复合控件果然简单呀,我都能学会~ using System; using System.Collection ...
- [原创] 在spring 中使用quarts
1.使用maven加载 quarts 的jar <dependency> <groupId>org.quartz-scheduler</groupId> <a ...