你的快樂的密碼:趣多多爆逗曲奇餅乾 920g 組合禮盒 49 元
阿新 • • 發佈:2022-04-15
BlockingQueue
方式 | 丟擲異常 | 有返回值,不丟擲異常 | 阻塞等待 | 超時等待 |
---|---|---|---|---|
新增 | boolean add(E e) | boolean offer(E e) | void put(E e) | boolean offer(E e, long timeout, TimeUnit unit) |
移除 | E remove() | E poll() | E take() | E poll(long timeout, TimeUnit unit) |
檢測隊首元素 | E element() | E peek() | - | - |
/* 丟擲異常 */ public static void test1() { // 引數:佇列的大小 ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3); System.out.println(blockingQueue.add("a")); System.out.println(blockingQueue.add("b")); System.out.println(blockingQueue.add("c")); // java.lang.IllegalStateException: Queue full 丟擲異常! // System.out.println(blockingQueue.add("d")); System.out.println("========================="); System.out.println(blockingQueue.remove()); System.out.println(blockingQueue.element());//檢視隊首元素是誰 System.out.println(blockingQueue.remove()); System.out.println(blockingQueue.remove()); // java.util.NoSuchElementException // System.out.println(blockingQueue.remove()); } /* 有返回值,沒有異常 */ public static void test2() { // 引數:佇列的大小 ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3); System.out.println(blockingQueue.offer("a")); System.out.println(blockingQueue.offer("b")); System.out.println(blockingQueue.offer("c")); // System.out.println(blockingQueue.offer("d"));//false 不丟擲異常 System.out.println(blockingQueue.peek()); System.out.println("==========================="); System.out.println(blockingQueue.poll()); System.out.println(blockingQueue.poll()); System.out.println(blockingQueue.poll()); System.out.println(blockingQueue.poll());//null 不丟擲異常 } /* 等待,阻塞(一直阻塞) */ public static void test3() throws InterruptedException { // 引數:佇列的大小 ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3); blockingQueue.put("a"); blockingQueue.put("b"); blockingQueue.put("c"); // blockingQueue.put("d");//佇列沒有位置,一直阻塞 System.out.println(blockingQueue.take()); System.out.println(blockingQueue.take()); System.out.println(blockingQueue.take()); System.out.println(blockingQueue.take()); } /* 等待,阻塞(等待超時) */ public static void test4() throws InterruptedException { // 引數:佇列的大小 ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3); blockingQueue.offer("a"); blockingQueue.offer("b"); blockingQueue.offer("c"); // blockingQueue.offer("d",2, TimeUnit.SECONDS);//等待超過兩秒就退出 System.out.println(blockingQueue.poll()); System.out.println(blockingQueue.poll()); System.out.println(blockingQueue.poll()); System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));//等待超過兩秒就退出 }
SynchronousQueue
/* 同步佇列 和其他的BlockingQueue不一樣,SynchronousQueue不儲存元素 put了一個元素,必須從裡面先take取出來,否則不能再put進去取! */ public class SynchronousQueueDemo { public static void main(String[] args) { BlockingQueue<String> blockingQueue = new SynchronousQueue<>();//同步佇列 new Thread(() -> { try { System.out.println(Thread.currentThread().getName() + " put 1"); blockingQueue.put("1"); System.out.println(Thread.currentThread().getName() + " put 2"); blockingQueue.put("2"); System.out.println(Thread.currentThread().getName() + " put 3"); blockingQueue.put("3"); } catch (InterruptedException e) { e.printStackTrace(); } }, "T1").start(); new Thread(() -> { try { TimeUnit.SECONDS.sleep(2); System.out.println(Thread.currentThread().getName() + "=>" + blockingQueue.take()); TimeUnit.SECONDS.sleep(2); System.out.println(Thread.currentThread().getName() + "=>" + blockingQueue.take()); TimeUnit.SECONDS.sleep(2); System.out.println(Thread.currentThread().getName() + "=>" + blockingQueue.take()); } catch (InterruptedException e) { e.printStackTrace(); } }, "T2").start(); } }