线程间通信之消费者模式一notifyAll()
来源:互联网 发布:新网域名管理权变更 编辑:程序博客网 时间:2024/04/29 03:02
public class ProducerConsumer
{
public static void main(String argsp[]){
Dug dug = new Dug();
Producer pro = new Producer(dug);
Customer cus = new Customer(dug);
Thread t1 = new Thread(pro,"员工一号");
Thread t2 = new Thread(pro,"员工二号");
Thread t3 = new Thread(cus,"消费一号");
Thread t4 = new Thread(cus,"消费二号");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Dug{
private int dugCount;
public synchronized void create(){
while(true){
while(dugCount>0){//将0改成flag或者改成9
try{
this.wait();
}catch(Exception e){
}
}
System.out.println(Thread.currentThread().getName()+"生产第"+(++dugCount)+"只烤鸭");
try{
this.notifyAll();
}catch(Exception e){
}
}
}
public synchronized void sale(){
while(true){
while(dugCount<1){
try{
this.wait();
}catch(Exception e){
}
}
System.out.println(Thread.currentThread().getName()+"消费第"+(dugCount--)+"只烤鸭");
try{
this.notifyAll();
}catch(Exception e){
}
}
}
}
class Producer implements Runnable
{
private Dug dug;
public Producer(Dug dug){
this.dug = dug;
}
public void run(){
dug.create();
}
}
class Customer implements Runnable
{
private Dug dug;
public Customer(Dug dug){
this.dug = dug;
}
public void run(){
dug.sale();
}
}
{
public static void main(String argsp[]){
Dug dug = new Dug();
Producer pro = new Producer(dug);
Customer cus = new Customer(dug);
Thread t1 = new Thread(pro,"员工一号");
Thread t2 = new Thread(pro,"员工二号");
Thread t3 = new Thread(cus,"消费一号");
Thread t4 = new Thread(cus,"消费二号");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Dug{
private int dugCount;
public synchronized void create(){
while(true){
while(dugCount>0){//将0改成flag或者改成9
try{
this.wait();
}catch(Exception e){
}
}
System.out.println(Thread.currentThread().getName()+"生产第"+(++dugCount)+"只烤鸭");
try{
this.notifyAll();
}catch(Exception e){
}
}
}
public synchronized void sale(){
while(true){
while(dugCount<1){
try{
this.wait();
}catch(Exception e){
}
}
System.out.println(Thread.currentThread().getName()+"消费第"+(dugCount--)+"只烤鸭");
try{
this.notifyAll();
}catch(Exception e){
}
}
}
}
class Producer implements Runnable
{
private Dug dug;
public Producer(Dug dug){
this.dug = dug;
}
public void run(){
dug.create();
}
}
class Customer implements Runnable
{
private Dug dug;
public Customer(Dug dug){
this.dug = dug;
}
public void run(){
dug.sale();
}
}
0 0
- 线程间通信之消费者模式一notifyAll()
- Java多线程之线程间通信--生产者/消费者模式
- 原版线程通信之生产消费者模式
- 多线程执行多类型任务(线程通信wait/notify(notifyAll)):生产者消费者模型(一)
- 线程间的通信-生产消费者模式
- 线程间通信之生产者消费者问题
- 多线程五,线程间通信3,wait、notify,notifyAll方法,生产者和消费者问题(14,毕向东老师)
- 【java线程系列】java线程系列之线程间的交互wait()/notify()/notifyAll()及生产者与消费者模型
- wait/notify/notifyAll实现线程间通信
- JAVA多线程-线程间通信(二)-生产者/消费者模式实现
- Java多线程编程3--线程间通信--生产者/消费者模式
- java线程通信 生产者与消费者模式
- 线程间通信-生产者消费者
- 线程通信之生产者消费者间wait()、notify实现
- 多线程执行多类型任务(线程通信wait/notify(notifyAll)):生产者消费者模型(二)
- java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)
- java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)
- 线程通信之生产者消费者详解
- PHP下Unix时间戳与日期互转(解决1970年前以及2038年后的问题)
- Aurigma Image Uploader的ActiveX / JAVA图像文件上传控件概述
- 怎么使用侧滑菜单SlidingMenu?
- hadoop hdfs
- 语句和数组
- 线程间通信之消费者模式一notifyAll()
- Common Sentences for Scientific Writing (8)
- Java专业人士必备的书籍和网站列表
- 随便玩了玩pb_ds库
- Linux I2C驱动分析与实现(一)
- Mysql多表left join查询
- 线程间通信之消费生产者模式二notify()
- Linux I2C驱动分析与实现(二)
- LINQ TO SQL