BlockingQueue生产者与消费者阻塞队列版参考

 1 import org.apache.tomcat.util.collections.SynchronizedQueue;
 2 
 3 import java.util.concurrent.ArrayBlockingQueue;
 4 import java.util.concurrent.BlockingQueue;
 5 import java.util.concurrent.SynchronousQueue;
 6 import java.util.concurrent.TimeUnit;
 7 import java.util.concurrent.atomic.AtomicInteger;
 8 
 9 /**
10  * 使用阻塞队列,不用对队列进行wait、notity等操作
11  */
12 
13 class MyResource{
14     private volatile  boolean FLAG=true;
15     private AtomicInteger num=new AtomicInteger();
16 
17     BlockingQueue<String> blockingQueue=null;
18 
19     public MyResource(BlockingQueue<String> blockingQueue) {
20         this.blockingQueue = blockingQueue;
21     }
22 
23     public void myProd() throws  Exception{
24         String data=null;
25         boolean off=false;
26         while (FLAG){
27             data = num.incrementAndGet()+"";
28             off=blockingQueue.offer(data,2L, TimeUnit.SECONDS);
29             if(off){
30                 System.out.println(Thread.currentThread().getName()+"\t 数据插入成功"+data);
31             }else{
32                 System.out.println(Thread.currentThread().getName()+"\t 数据插入失败"+data);
33             }
34             //TimeUnit.SECONDS.sleep(1);
35         }
36     }
37     public void myConsumer() throws  Exception{
38         String poll=null;
39 
40         while (FLAG){
41             poll = blockingQueue.poll(2L, TimeUnit.SECONDS);
42             if(poll==null||poll.equalsIgnoreCase("")){
43                 FLAG=false;
44                 System.out.println(Thread.currentThread().getName()+"\t 数据消费失败");
45                 return ;
46             }
47             System.out.println(Thread.currentThread().getName()+"\t 数据消费成功"+poll);
48             System.out.println();
49             System.out.println();
50             TimeUnit.SECONDS.sleep(1);
51         }
52     }
53     public void getStatus(){
54         System.out.println(Thread.currentThread().getName()+"\t 主线程叫停");
55         FLAG=false;
56     }
57 }
58 
59 
60 public class ProdConsumer_BlockQueueDemo {
61     public static void main(String[] args) throws  Exception{
62         BlockingQueue<String> blockingQueue=new ArrayBlockingQueue<>(10);
63         //BlockingQueue synchronousQueue=new SynchronousQueue();
64         MyResource resource = new MyResource(blockingQueue);
65 
66         new Thread(()->{
67             System.out.println(Thread.currentThread().getName()+"\t 生产者线程启动");
68             try {
69                 resource.myProd();
70             } catch (Exception e) {
71                 e.printStackTrace();
72             }
73         },"Prod").start();
74         new Thread(()->{
75             System.out.println(Thread.currentThread().getName()+"\t 消费者线程启动");
76             try {
77                 resource.myConsumer();
78             } catch (Exception e) {
79                 e.printStackTrace();
80             }
81         },"Consumer").start();
82 
83         TimeUnit.SECONDS.sleep(5);
84         resource.getStatus();
85     }
86 }

 

posted @ 2021-05-09 14:19  ffzzblog  阅读(87)  评论(0)    收藏  举报