[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





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。