服务器之家

服务器之家 > 正文

JAVA多线程实现生产者消费者的实例详解

时间:2020-11-12 17:24     来源/作者:m1457285665

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

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部