欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA多線程實(shí)現(xiàn)生產(chǎn)者消費(fèi)者的實(shí)例詳解

 更新時(shí)間:2017年06月09日 09:18:05   作者:m1457285665  
這篇文章主要介紹了JAVA多線程實(shí)現(xiàn)生產(chǎn)者消費(fèi)者的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

JAVA多線程實(shí)現(xiàn)生產(chǎn)者消費(fèi)者的實(shí)例詳解

下面的代碼實(shí)現(xiàn)了生產(chǎn)者消費(fèi)者的問題

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(); 
      
   } 
  } 
 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • WeakHashMap?和?HashMap?區(qū)別及使用場景

    WeakHashMap?和?HashMap?區(qū)別及使用場景

    這篇文章主要為大家介紹了WeakHashMap?和?HashMap?的區(qū)別是什么以及何時(shí)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • java多線程編程實(shí)例

    java多線程編程實(shí)例

    這篇文章主要介紹了java多線程編程實(shí)例,分享了幾則多線程的實(shí)例代碼,具有一定參考價(jià)值,加深多線程編程的理解還是很有幫助的,需要的朋友可以參考下。
    2017-11-11
  • Java實(shí)現(xiàn)PDF文件的分割與加密功能

    Java實(shí)現(xiàn)PDF文件的分割與加密功能

    這篇文章主要為大家分享了如何利用Java語言實(shí)現(xiàn)PDF文件的分割與加密以及封面圖的生成,文中的示例代碼簡潔易懂,感興趣的可以了解一下
    2022-04-04
  • 詳解Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性

    詳解Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性

    Chrome 51 開始,瀏覽器的 Cookie 新增加了一個(gè)SameSite屬性,用來防止 CSRF 攻擊和用戶追蹤。今天通過本文給大家介紹Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性,感興趣的朋友一起看看吧
    2022-01-01
  • springboot業(yè)務(wù)功能實(shí)戰(zhàn)之告別輪詢websocket的集成使用

    springboot業(yè)務(wù)功能實(shí)戰(zhàn)之告別輪詢websocket的集成使用

    WebSocket使得客戶端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡單,允許服務(wù)端主動向客戶端推送數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于springboot業(yè)務(wù)功能實(shí)戰(zhàn)之告別輪詢websocket的集成使用,需要的朋友可以參考下
    2022-10-10
  • 淺析SpringBoot微服務(wù)中異步調(diào)用數(shù)據(jù)提交數(shù)據(jù)庫的問題

    淺析SpringBoot微服務(wù)中異步調(diào)用數(shù)據(jù)提交數(shù)據(jù)庫的問題

    這篇文章主要介紹了SpringBoot微服務(wù)中異步調(diào)用數(shù)據(jù)提交數(shù)據(jù)庫的問題,今天本文涉及到的知識點(diǎn)不難,都是很簡單的crud操作,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • SpringBoot增量/瘦身部署jar包的方式

    SpringBoot增量/瘦身部署jar包的方式

    SpringBoot 項(xiàng)目的部署一般采用全量jar 包方式部署相關(guān)項(xiàng)目,如果我們對相關(guān)的Contrller層進(jìn)行相關(guān)業(yè)務(wù)調(diào)整就需要重新編譯全量jar 包太麻煩了,所以本文給大家介紹了使用SpringBoot 的增量/瘦身部署方式,需要的朋友可以參考下
    2024-01-01
  • 解決idea中maven新增的配置文件xx.xml沒生效問題

    解決idea中maven新增的配置文件xx.xml沒生效問題

    這篇文章主要介紹了如何解決idea中maven新增的配置文件xx.xml沒生效問題,公司項(xiàng)目有用自己的`私服,Maven正常去私服下載jar包是沒問題的,但阿里云鏡像找不到相關(guān)的jar包報(bào)錯(cuò),文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-06-06
  • 使用spring容器在初始化Bean時(shí)前和后的操作

    使用spring容器在初始化Bean時(shí)前和后的操作

    這篇文章主要介紹了使用spring容器在初始化Bean時(shí)前和后的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java 同步器SynchronousQueue詳解及實(shí)例

    java 同步器SynchronousQueue詳解及實(shí)例

    這篇文章主要介紹了java 同步器SynchronousQueue詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05

最新評論