码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-06-13 20:05:47      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

多线程: 生产与消费
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基础] java多线程关于消费者和生产者

标签:

原文地址:http://www.cnblogs.com/chengzhengfu/p/4573918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!