java 多线程生产者_java-Runnable加锁实现生产者和消费者的多线程问题
//庫存函數,保存著庫存的信息Storage.java
public classStorage {//模擬庫存
public Integer num=1;
}//生產者函數 product.java/************************************************************
Copyright?(C),?1988-1999,?Huawei?Tech.?Co.,?Ltd.
FileName:?Customer.java
Author:?????Light
Version?:????version1.0
Date:??? 2018/7/11
Description:????生產者進行生產,生產者需要在消費者進行消費后生產,模擬的內存只有一塊,
當沒有消費時,會提醒消費者消費,喚醒消費者進程 //?模塊描述
Version:?????? 1.0//?版本信息
Function?List:??? //?主要函數及其功能
1.
History:
//?歷史修改記錄
Light????2018/7/11?????1.0?????build?this?moudle
***********************************************************/
public class Product implementsRunnable{privateStorage storage;publicProduct(Storage storage) {super();this.storage =storage;
}
@Overridepublic voidrun() {//TODO Auto-generated method stub
while(true){synchronized(storage) {if(this.storage.num<1){//模擬生產
this.storage.num++;
System.out.println("生產一個");//喚醒隊列中所有進程
this.storage.notifyAll();
}else{try{//進入等待隊列
this.storage.wait();
}catch(InterruptedException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}//消費者函數 Customer.java/************************************************************
Copyright?(C),?1988-1999,?Huawei?Tech.?Co.,?Ltd.
FileName:?Customer.java
Author:?????Light
Version?:????version1.0
Date:??? 2018/7/11
Description:????消費者進行消費,消費者需要在生產者進行生產再進行消費,
當沒有生產時,會提醒生產者生產,喚醒生產者進程 //?模塊描述
Version:?????? 1.0//?版本信息
Function?List:??? //?主要函數及其功能
1.
History:
//?歷史修改記錄
Light????2018/7/11?????1.0?????build?this?moudle
***********************************************************/
public class Customer implementsRunnable {privateStorage storage;publicCustomer(Storage storage) {super();this.storage =storage;
}/*** 重寫run()
*@author[Light](必須)
*@see[run()](可選)
*@since[version 1.0] (必須)*/@Overridepublic voidrun() {//TODO Auto-generated method stub
while(true){//加鎖
synchronized(storage) {//判斷是否生產者進行了生產
if(this.storage.num>=1){//模擬消費
this.storage.num--;
System.out.println("消費一個");//喚醒隊列所有進程
this.storage.notifyAll();
}else{try{//等待
this.storage.wait();
}catch(InterruptedException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}//主函數 MyTest.java/************************************************************
Copyright?(C),?1988-1999,?Huawei?Tech.?Co.,?Ltd.
FileName:?MyTest.java
Author:?????Light
Version?:????version1.0
Date:??? 2018/7/11
Description:???消費生產進程模擬 //?模塊描述
Version:?????? 1.0//?版本信息
Function?List:??? //?主要函數及其功能
1.
History:
//?歷史修改記錄
Light????2018/7/11?????1.0?????build?this?moudle
***********************************************************/
public classMyTest {public static voidmain(String[] args) {
//庫存
Storage storage=newStorage();
//生產者
Product p1=newProduct(storage);
//兩名消費者
Customer c1=newCustomer(storage);
Customer c2=newCustomer(storage);
Thread t1=newThread(p1);
Thread t2=newThread(c1);
Thread t3=newThread(c2);
t1.start();
t2.start();
t3.start();
}
}
總結
以上是生活随笔為你收集整理的java 多线程生产者_java-Runnable加锁实现生产者和消费者的多线程问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: String类得常用方法
- 下一篇: dosbox运行C语言,DOSBox怎么