JAVA多線(xiàn)程實(shí)現(xiàn)生產(chǎn)者消費(fèi)者的實(shí)例詳解
JAVA多線(xiàn)程實(shí)現(xiàn)生產(chǎn)者消費(fèi)者的實(shí)例詳解
下面的代碼實(shí)現(xiàn)了生產(chǎn)者消費(fèi)者的問(wèn)題
Product.Java
package consumerProducer;
public class Product {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Product(String id)
{
this.id=id;
}
public String toString()
{
return "product "+id;
}
}
Pool.java
package consumerProducer;
import java.util.*;
public class Pool {
private int number=0;
private List<Product>products=new LinkedList<Product>();
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public synchronized Product consumeProduct(){ //可以去掉synchronized關(guān)鍵字
if(products.size()>0)
{ Product p= products.get(0);
products.remove(0);
number--;
return p;
}
else
return null;
}
public synchronized void addProduct(Product p){ //可以去掉synchronized關(guān)鍵字
products.add(p);
number++;
}
}
Consumer.java
package consumerProducer;
public class Consumer implements Runnable {
private String id;
Pool pool;
public Consumer(String id,Pool pool)
{
this.id=id;
this.pool=pool;
}
@Override
public void run() {
while(!Thread.currentThread().interrupted())
{
Product product=null;
synchronized(pool){
while(pool.getNumber()<=0)//生產(chǎn)不足
{
try {
pool.wait();//生產(chǎn)者等待
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
product=pool.consumeProduct();
}
System.out.println("consuming "+id+product.toString());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Producer.java
package consumerProducer;
public class Producer implements Runnable{
private int i_p=0;
private String id;
Pool pool;
int i=0;
public Producer(String id ,Pool pool)
{
this.id=id;
this.pool=pool;
}
public Product createProduct()
{
return new Product(String.valueOf(++i_p));
}
@Override
public void run() {
// TODO Auto-generated method stub
while(!Thread.currentThread().interrupted())
{
Product p=new Product(String.valueOf(++i_p));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized(pool)
{
pool.addProduct(p);
System.out.println("producer "+id+" adding product...."+p.toString());
pool.notifyAll();
}
}
}
}
Main.java
package consumerProducer;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Pool pool=new Pool();
for(int i=0;i<5;i++)
{
Thread consumer=new Thread(new Consumer("consumer "+i,pool));
Thread producer=new Thread(new Producer("producer "+i,pool));
consumer.start();
producer.start();
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
WeakHashMap?和?HashMap?區(qū)別及使用場(chǎng)景
這篇文章主要為大家介紹了WeakHashMap?和?HashMap?的區(qū)別是什么以及何時(shí)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Java實(shí)現(xiàn)PDF文件的分割與加密功能
這篇文章主要為大家分享了如何利用Java語(yǔ)言實(shí)現(xiàn)PDF文件的分割與加密以及封面圖的生成,文中的示例代碼簡(jiǎn)潔易懂,感興趣的可以了解一下2022-04-04
詳解Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性
Chrome 51 開(kāi)始,瀏覽器的 Cookie 新增加了一個(gè)SameSite屬性,用來(lái)防止 CSRF 攻擊和用戶(hù)追蹤。今天通過(guò)本文給大家介紹Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性,感興趣的朋友一起看看吧2022-01-01
springboot業(yè)務(wù)功能實(shí)戰(zhàn)之告別輪詢(xún)websocket的集成使用
WebSocket使得客戶(hù)端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡(jiǎn)單,允許服務(wù)端主動(dòng)向客戶(hù)端推送數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于springboot業(yè)務(wù)功能實(shí)戰(zhàn)之告別輪詢(xún)websocket的集成使用,需要的朋友可以參考下2022-10-10
淺析SpringBoot微服務(wù)中異步調(diào)用數(shù)據(jù)提交數(shù)據(jù)庫(kù)的問(wèn)題
這篇文章主要介紹了SpringBoot微服務(wù)中異步調(diào)用數(shù)據(jù)提交數(shù)據(jù)庫(kù)的問(wèn)題,今天本文涉及到的知識(shí)點(diǎn)不難,都是很簡(jiǎn)單的crud操作,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
解決idea中maven新增的配置文件xx.xml沒(méi)生效問(wèn)題
這篇文章主要介紹了如何解決idea中maven新增的配置文件xx.xml沒(méi)生效問(wèn)題,公司項(xiàng)目有用自己的`私服,Maven正常去私服下載jar包是沒(méi)問(wèn)題的,但阿里云鏡像找不到相關(guān)的jar包報(bào)錯(cuò),文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
使用spring容器在初始化Bean時(shí)前和后的操作
這篇文章主要介紹了使用spring容器在初始化Bean時(shí)前和后的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
java 同步器SynchronousQueue詳解及實(shí)例
這篇文章主要介紹了java 同步器SynchronousQueue詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05

