一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - JAVA多線程實現生產者消費者的實例詳解

JAVA多線程實現生產者消費者的實例詳解

2020-11-12 17:24m1457285665 Java教程

這篇文章主要介紹了JAVA多線程實現生產者消費者的實例詳解的相關資料,需要的朋友可以參考下

JAVA多線程實現生產者消費者的實例詳解

下面的代碼實現了生產者消費者的問題

Product.Java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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關鍵字
  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關鍵字
       
      products.add(p);
      number++;
}
 
}

Consumer.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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)//生產不足
        {
          try {
            pool.wait();//生產者等待
          } 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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();
      
   }
  }
 
}

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

原文鏈接:http://blog.csdn.net/m1457285665/article/details/44501557

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品免费观看 | 男人女人日皮 | 1769在线视频| bestialitysex杂交 bedfriend泰剧全集免费观看 | 女仆色永久免费网站 | 2021国产精品视频一区 | yy8090韩国日本三理论免费 | 人人爱天天做夜夜爽88 | 国产精品女主播大秀在线 | 嫩草在线观看视频 | chinese男性厕所撒尿合集 | 三级无删减高清在线影院 | 色婷婷久久综合中文久久一本 | 激情涩涩| ysl千人千色t9t9t9| 国产三级自拍视频 | 午夜熟女插插XX免费视频 | 亚洲欧美综合人成野草 | 私人黄色影院 | chinese调教踩踏视频 | 青青草精品在线观看 | 妹妹骑上来蹭着蹭着就射了 | 成人一级黄色大片 | 美女翘臀跪床被打屁股作文 | 香蕉97超级碰碰碰免费公 | 教室里的激情电影 | 32pao强力打造免费高速高清 | 狠狠躁夜夜躁人人爽天天miya | 色婷婷影院在线视频免费播放 | 免费日本在线视频 | 超碰av | 污黄漫 | 2012在线观看免费视频大全 | a4yy欧美一区二区三区 | 男人把j放进女人的p里视频 | 国产91精品区 | 国产成人精品本亚洲 | ipx358cn出差被男上司在线 | 韩国三级年轻小的胰子完整 | 乌克兰粉嫩摘花第一次 | 成年人免费观看视频网站 |