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

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - JAVA多線程實現(xiàn)生產(chǎn)者消費者的實例詳解

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

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

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

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

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

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關(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

?
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)//生產(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

?
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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产成人免费片在线观看 | 免费看视频 | 99久久精品6在线播放 | 亚洲国产99在线精品一区69堂 | 故意短裙公车被强好爽在线播放 | 男人使劲躁女人视频免费 | oneday日本在线观看完整版 | 美女污视频 | 女主被男主做哭失禁高h | eee在线播放成人免费 | 大逼美女 | 天天舔天天干 | 贵妇的私人性俱乐部 | 精久久 | 日本阿v精品视频在线观看 日本xxx片免费高清在线 | 国产51| 第一国内永久免费福利视频 | 岛国在线播放v片免费 | www.青青草原| 亚洲酒色1314狠狠做 | 成年人免费观看的视频 | 国产精品污双胞胎在线观看 | 四虎私人影院 | 三级黄色图片 | 欧美一级片免费 | 国产51| 国产视频自拍一区 | 无码毛片内射白浆视频 | 农夫69小说小雨与农村老太 | 99在线视频观看 | 四虎在线永久免费视频网站 | 日本不卡视频免费的 | 青草免费在线 | 精品国产福利片在线观看 | 欧亚精品一区二区三区 | 秋霞鲁丝影院久久人人综合 | 日韩成人在线免费视频 | 91成人爽a毛片一区二区 | 草草在线影院 | 金牛网155755水心论坛黄大父母 | 6个老师的尿奴 |