Java并发编程之阻塞队列
生活随笔
收集整理的這篇文章主要介紹了
Java并发编程之阻塞队列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java并發編程之阻塞隊列
目錄
1. 阻塞隊列概述
2. 為什么用?有什么好處?
- 好處是我們不需要關心什么時候需要阻塞線程,什么時候需要喚醒線程,因為這一切BlockingQueue都給你一手包辦了
這會給我們的程序帶來不小的復雜度。
3. BlockingQueue的核心方法
1. 核心方法概述
2. 核心方法api使用
1. 阻塞隊列api之拋出異常組
代碼:
public class BlockingQueueDemo {public static void main(String[] args) {BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);System.out.println("添加元素" + blockingQueue.add("a"));System.out.println("添加元素" + blockingQueue.add("b"));System.out.println("添加元素" + blockingQueue.add("c")); // System.out.println("添加元素" + blockingQueue.add("d"));//檢查隊列第一個元素System.out.println(blockingQueue.element());System.out.println("移除元素" + blockingQueue.remove());System.out.println("移除元素" + blockingQueue.remove());System.out.println("移除元素" + blockingQueue.remove()); // System.out.println("移除元素"+blockingQueue.remove());} }1.1 正常執行結果:
1.2 將注釋的代碼System.out.println("添加元素" + blockingQueue.add("d"));打開。即:
執行結果:
報異常:java.lang.IllegalStateException: Queue full
1.3 將注釋的代碼System.out.println("移除元素" + blockingQueue.remove());打開,即:
執行結果:
報異常:Exception in thread “main” java.util.NoSuchElementException
1.4 小結:
當阻塞隊列空時,再往隊列里remove移除元素會拋出Exception in thread “main” java.util.NoSuchElementException異常。
2. 阻塞隊列api之返回布爾值組
代碼:
public static void main(String[] args) throws InterruptedException { System.out.println(blockingQueue.offer("a"));BlockingQueue<String> 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("a"));System.out.println("隊列首個元素:"+blockingQueue.peek());System.out.println("移除元素"+blockingQueue.poll());System.out.println("移除元素"+blockingQueue.poll());System.out.println("移除元素"+blockingQueue.poll());System.out.println("移除元素"+blockingQueue.poll());2.1 執行結果:
2.2 小結:
3. 阻塞隊列api之阻塞
代碼:
public static void main(String[] args) throws InterruptedException {BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);blockingQueue.put("a");blockingQueue.put("a");blockingQueue.put("a");//blockingQueue.put("a");System.out.println("==================");blockingQueue.take();blockingQueue.take();blockingQueue.take();//blockingQueue.take();}當打開注釋blockingQueue.put("a");或者blockingQueue.take();
執行結果:
小結:
4. 阻塞隊列api之超時控制
代碼:
public static void main(String[] args) throws InterruptedException {BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);System.out.println(blockingQueue.offer("a", 2, TimeUnit.SECONDS));System.out.println(blockingQueue.offer("a", 2, TimeUnit.SECONDS));System.out.println(blockingQueue.offer("a", 2, TimeUnit.SECONDS));System.out.println(blockingQueue.offer("a", 2, TimeUnit.SECONDS));}執行結果:
先阻塞兩秒鐘,2秒后自動結束
4. SynchronousQueue的用法
代碼實例:
public static void main(String[] args) throws Exception {BlockingQueue<String> blockingQueue = new SynchronousQueue<>();new Thread(() -> {try {System.out.println(Thread.currentThread().getName()+"\t put 1");blockingQueue.put("1");System.out.println(Thread.currentThread().getName()+"\t put 2");blockingQueue.put("2");System.out.println(Thread.currentThread().getName()+"\t put 3");blockingQueue.put("3");}catch (Exception e){e.printStackTrace();}},"AAA").start();new Thread(() -> {try {//暫停一會線程try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName()+"\t"+blockingQueue.take());try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName()+"\t"+blockingQueue.take());try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName()+"\t"+blockingQueue.take());}catch (Exception e){e.printStackTrace();}},"BBB").start();}執行結果:
5. 用在哪里
主要用在:
等后面寫完了再補上鏈接
總結
以上是生活随笔為你收集整理的Java并发编程之阻塞队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java锁之自旋锁
- 下一篇: Java多线程之Synchronized