[Java基础] java多线程关于消费者和生产者-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

[Java基础] java多线程关于消费者和生产者

简介:
多线程: 生产与消费

1.生产者Producer生产produce产品,并将产品放到库存inventory里;同时消费者Consumer从库存inventory里消费consume产品。

2.库存inventory的总量(最大库存量100)是有限的。如果库存inventory满了,生产者不能在库存inventory继续生产produce产品,须等待状态。等待产品被消费者Consumer消费consume了,再往库存inventory生产produce产品。

3.若库存inventory空了,消费者Consumer也须等待状态。等待生产者生产produce产品,再将消费者Consumer唤醒进行消费consume。

补充要求:需要6个线程,分别实现
1.库存为0;2.消费1消费10个(但库存零,则需等待);3.生产2生产5个;4.生产3生产5个(此时库存有10个,则满足消费1的消费;)最终库存仍有0个;5.生产4生产100个);6.生产5生产10个(因库存满了,无法生产,需等待)

复制代码
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
 
public class BlockingQueueTest {
 public static void main(String[] args) {
  ExecutorService service = Executors.newCachedThreadPool();
  BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<String>(100);
  System.out.println("blockingQueue now contains "  + blockingQueue.size() + " unit");
  service.submit(new Consumer1(blockingQueue));
  gap(blockingQueue);
  service.submit(new Productor2(blockingQueue));
  gap(blockingQueue);
  service.submit(new Productor3(blockingQueue));
  gap(blockingQueue);
  service.submit(new Productor4(blockingQueue));
  gap(blockingQueue);
  service.submit(new Productor5(blockingQueue));
  gap(blockingQueue);
   
  service.shutdown();
 }
 private static void gap(BlockingQueue<String> blockingQueue) {
  try {
   Thread.sleep(1000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println("blockingQueue now contains "  + blockingQueue.size() + " unit");
 }
}
class Consumer1 implements Runnable{
 BlockingQueue<String> blockingQueue;
  
 public Consumer1(BlockingQueue<String> blockingQueue) {
  super();
  this.blockingQueue = blockingQueue;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  System.out.println("Consumer1 start: need 10 units");
  for(int i = 0; i < 10; i++){
   try {
    blockingQueue.take();
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  System.out.println("Consumer1 end: has got 10 units");
 }
  
}
class Productor2 implements Runnable{
 BlockingQueue<String> blockingQueue;
  
 public Productor2(BlockingQueue<String> blockingQueue) {
  super();
  this.blockingQueue = blockingQueue;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  System.out.println("Productor2 start: put 5 units");
  for(int i = 0; i < 5; i++){
   try {
    blockingQueue.put("Object");
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  System.out.println("Productor2 end: has put 5 units");
 }
  
}
class Productor3 implements Runnable{
 BlockingQueue<String> blockingQueue;
  
 public Productor3(BlockingQueue<String> blockingQueue) {
  super();
  this.blockingQueue = blockingQueue;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  System.out.println("Productor3 start: put 5 units");
  for(int i = 0; i < 5; i++){
   try {
    blockingQueue.put("Object");
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  System.out.println("Productor3 end: has put 5 units");
 }
  
}
class Productor4 implements Runnable{
 BlockingQueue<String> blockingQueue;
  
 public Productor4(BlockingQueue<String> blockingQueue) {
  super();
  this.blockingQueue = blockingQueue;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  System.out.println("Productor4 start: put 100 units");
  for(int i = 0; i < 100; i++){
   try {
    blockingQueue.put("Object");
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  System.out.println("Productor4 end: has put 100 units");
 }
  
}
class Productor5 implements Runnable{
 BlockingQueue<String> blockingQueue;
  
 public Productor5(BlockingQueue<String> blockingQueue) {
  super();
  this.blockingQueue = blockingQueue;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  System.out.println("Productor5 start: put 10 units");
  for(int i = 0; i < 100; i++){
   try {
    blockingQueue.put("Object");
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  System.out.println("Productor5 end: has put 10 units");
 }
  
}
复制代码

每个线程是隔了1s启动的, 结果

blockingQueue now contains 0 unit
Consumer1 start: need 10 units
blockingQueue now contains 0 unit
Productor2 start: put 5 units
Productor2 end: has put 5 units
blockingQueue now contains 0 unit
Productor3 start: put 5 units
Productor3 end: has put 5 units
Consumer1 end: has got 10 units
blockingQueue now contains 0 unit
Productor4 start: put 100 units
Productor4 end: has put 100 units
blockingQueue now contains 100 unit
Productor5 start: put 10 units
blockingQueue now contains 100 unit





分类: Java基础
本文转自demoblog博客园博客,原文链接http://www.cnblogs.com/0616--ataozhijia/p/4126164.html如需转载请自行联系原作者

demoblog

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享: