码迷,mamicode.com
首页 > 其他好文 > 详细

BlockingQueue->ArrayBlockingQueue/SynchronousQueue

时间:2021-05-24 02:13:16      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:array   rac   str   interrupt   read   time   code   port   public   

 2 
 3 import java.util.concurrent.ArrayBlockingQueue;
 4 import java.util.concurrent.BlockingQueue;
 5 import java.util.concurrent.LinkedBlockingDeque;
 6 import java.util.concurrent.TimeUnit;
 7 
 8 /**
 9  * BlockQueue是Collection下的接口,与List同级别
10  */
11 public class BlockingQueueDemo {
12     public static void main(String[] args) {
13         BlockingQueue<String> blockingQueue=new ArrayBlockingQueue<>(3);
14         try {
15             System.out.println(blockingQueue.offer("a", 2L, TimeUnit.SECONDS));
16             System.out.println(blockingQueue.offer("a", 2L, TimeUnit.SECONDS));
17             System.out.println(blockingQueue.offer("a", 2L, TimeUnit.SECONDS));
18             System.out.println(blockingQueue.offer("a", 2L, TimeUnit.SECONDS));//false等2s,如果不能获取返回退出
19         } catch (InterruptedException e) {
20             e.printStackTrace();
21         }
22 
23     }
24 
25     private static void theThirdGroup() {
26         //阻塞
27         BlockingQueue<String> blockingQueue=new ArrayBlockingQueue<>(3);
28         try {
29             blockingQueue.put("a");
30             blockingQueue.put("a");
31             blockingQueue.put("a");
32             //blockingQueue.put("a");//阻塞
33         } catch (InterruptedException e) {
34             e.printStackTrace();
35         }
36 
37         try {
38             System.out.println(blockingQueue.take());
39             System.out.println(blockingQueue.take());
40             System.out.println(blockingQueue.take());
41             System.out.println(blockingQueue.take());//阻塞
42         } catch (InterruptedException e) {
43             e.printStackTrace();
44         }
45     }
46 
47     private static void theSecondGroup() {
48         //特殊值
49         BlockingQueue<String> blockingQueue=new ArrayBlockingQueue<>(3);
50         System.out.println(blockingQueue.offer("a"));
51         System.out.println(blockingQueue.offer("b"));
52         System.out.println(blockingQueue.offer("c"));
53         System.out.println(blockingQueue.offer("d"));//false
54 
55         System.out.println(blockingQueue.peek());//a
56 
57         System.out.println(blockingQueue.poll());
58         System.out.println(blockingQueue.poll());
59         System.out.println(blockingQueue.poll());
60         System.out.println(blockingQueue.poll());//null
61     }
62 
63     private static void theFirstGroup() {
64         BlockingQueue<String> blockingQueue=new ArrayBlockingQueue<>(3);
65         /**
66          * 第一组
67          * add()添加元素 remove()移除
68          */
69         System.out.println(blockingQueue.add("a"));
70         System.out.println(blockingQueue.add("b"));
71         System.out.println(blockingQueue.add("c"));
72         //blockingQueue.add("d");//Exception in thread "main" java.lang.IllegalStateException: Queue full
73 
74         System.out.println(blockingQueue.element());
75 
76         System.out.println(blockingQueue.remove());
77         System.out.println(blockingQueue.remove());
78         System.out.println(blockingQueue.remove());
79         //System.out.println(blockingQueue.remove());//Exception in thread "main" java.util.NoSuchElementException
80     }
81 }
 3 import java.util.concurrent.BlockingQueue;
 4 import java.util.concurrent.SynchronousQueue;
 5 import java.util.concurrent.TimeUnit;
 6 
 7 public class SynchronousQueueDemo {
 8     public static void main(String[] args) {
 9         BlockingQueue synchronousQueue=new SynchronousQueue();
10         new Thread(()->{
11             try {
12                 System.out.println(Thread.currentThread().getName()+"\t put 1");
13                 synchronousQueue.put("1");
14 
15                 System.out.println(Thread.currentThread().getName()+"\t put 2");
16                 synchronousQueue.put("2");
17 
18                 System.out.println(Thread.currentThread().getName()+"\t put 3");
19                 synchronousQueue.put("3");
20             } catch (InterruptedException e) {
21                 e.printStackTrace();
22             }
23         },"AAA").start();
24         new Thread(()->{
25             try {
26                 TimeUnit.SECONDS.sleep(5);
27                 System.out.println(synchronousQueue.take());
28                 TimeUnit.SECONDS.sleep(5);
29                 System.out.println(synchronousQueue.take());
30                 TimeUnit.SECONDS.sleep(5);
31                 System.out.println(synchronousQueue.take());
32 
33             } catch (InterruptedException e) {
34                 e.printStackTrace();
35             }
36         },"BBB").start();
37 
38 
39 
40     }
41 }

 

BlockingQueue->ArrayBlockingQueue/SynchronousQueue

标签:array   rac   str   interrupt   read   time   code   port   public   

原文地址:https://www.cnblogs.com/ffzzcommsoft/p/14746241.html

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