從頭認識java-17.5 阻塞隊列(以生產者消費者模式為例)
這一章節我們來討論一下阻塞隊列。我們下面將通過生產者消費者模式來介紹阻塞隊列。
1.什麼是阻塞隊列?(摘自於並發編程網對http://tutorials.jenkov.com/java-concurrency/blocking-queues.html的翻譯)
當隊列是空的時,從隊列中擷取元素的操作將會被阻塞,或者當隊列是滿時,往隊列裡添加元素的操作會被阻塞。試圖從空的阻塞隊列中擷取元素的線程將會被阻塞,直到其他的線程往空的隊列插入新的元素。同樣,試圖往已滿的阻塞隊列中添加新元素的線程同樣也會被阻塞,直到其他的線程使隊列重新變得空閑起來,如從隊列中移除一個或者多個元素,或者完全清空隊列,展示了如何通過阻塞隊列來合作:
2.特性
(1)先進先出
(2)線程同步
3.下面結合生產者消費者模式來介紹阻塞隊列的使用方式
package com.ray.ch17;import java.util.concurrent.LinkedBlockingQueue;public class Test {public static void main(String[] args) {Basket basket = new Basket();Thread thread1 = new Thread(new Chief(basket));Thread thread2 = new Thread(new Customer(basket));Thread thread3 = new Thread(new Customer(basket));thread1.start();thread2.start();thread3.start();}}class Cake {private static int index = 0;private final int id = index++;public int getId() {return id;}public Cake() {System.out.println("生產了蛋糕,id:" + id);}}class Chief implements Runnable {private Basket basket = null;public Chief(Basket basket) {this.basket = basket;}public void makeCake() throws InterruptedException {for (int i = 0; i < 20; i++) {basket.put(new Cake());System.out.println("現在框裡面的蛋糕數量:" + basket.getQueueSize());Thread.sleep(20);}}@Overridepublic void run() {try {makeCake();} catch (InterruptedException e) {e.printStackTrace();}}}class Customer implements Runnable {private Basket basket = null;public Customer(Basket basket) {this.basket = basket;}public void buyCake() throws InterruptedException {System.out.println("消費了蛋糕,id:" + basket.take().getId());System.out.println("現在框裡面的蛋糕數量:" + basket.getQueueSize());}@Overridepublic void run() {try {for (int i = 0; i < 10; i++) {buyCake();Thread.sleep(100);}} catch (InterruptedException e) {e.printStackTrace();}}}class Basket {private LinkedBlockingQueue queue = new LinkedBlockingQueue(10);public void put(Cake cake) throws InterruptedException {queue.put(cake);}public Cake take() throws InterruptedException {Cake cake = queue.take();return cake;}public int getQueueSize() {return queue.size();}}輸出:
生產了蛋糕,id:0
消費了蛋糕,id:0
現在框裡面的蛋糕數量:0
現在框裡面的蛋糕數量:0
生產了蛋糕,id:1
現在框裡面的蛋糕數量:1
消費了蛋糕,id:1
現在框裡面的蛋糕數量:0
生產了蛋糕,id:2
現在框裡面的蛋糕數量:1
生產了蛋糕,id:3
現在框裡面的蛋糕數量:2
生產了蛋糕,id:4
現在框裡面的蛋糕數量:3
消費了蛋糕,id:2
現在框裡面的蛋糕數量:2
生產了蛋糕,id:5
現在框裡面的蛋糕數量:3
消費了蛋糕,id:3
現在框裡面的蛋糕數量:2
生產了蛋糕,id:6
現在框裡面的蛋糕數量:3
生產了蛋糕,id:7
現在框裡面的蛋糕數量:4
生產了蛋糕,id:8
現在框裡面的蛋糕數量:5
生產了蛋糕,id:9
現在框裡面的蛋糕數量:6
消費了蛋糕,id:4
現在框裡面的蛋糕數量:5
消費了蛋糕,id:5
現在框裡面的蛋糕數量:4
消費了蛋糕,id:6
現在框裡面的蛋糕數量:3
消費了蛋糕,id:7
現在框裡面的蛋糕數量:2
消費了蛋糕,id:8
現在框裡面的蛋糕數量:1
消費了蛋糕,id:9
現在框裡面的蛋糕數量:0
解釋:
(1)由於是生產者消費者模式,因此上面必須有生產者、消費者、籃子和消費的物品(蛋糕)這四個類,還有一個Test測試類別
(2)在Cake類裡面,我們使用static的index來記錄每一個Cake的id
(3)在Basket類裡面我們除了put和take方法,還放入一個getSize的方法來得到籃子裡面現在有多少個Cake
(4)生產者和消費者其實只有一個方法就是生產make或者buy,這裡面需要注意的是兩個地方,第一個是數量,由於我們上面使用的是阻塞隊列,因此當生產的總數量大於消費的總數量,程式就會一直在wait,只有等到下一個消費者來消費才會繼續執行,反之,如果消費的數量大於生產的數量也會出現這種情況;第二個是時間,我們需要按照實際的時間匹配關係來配對時間,如果生產大於消費,在出現生產一個消費一個,需要增加生產者,如果是消費大於生產,則根據實際需求來定。
下面的代碼需要等待,就是因為消費的總數大於生產的總數而出現的問題:
package com.ray.ch17;import java.util.concurrent.LinkedBlockingQueue;public class Test {public static void main(String[] args) {Basket basket = new Basket();Thread thread1 = new Thread(new Chief(basket));Thread thread2 = new Thread(new Customer(basket));Thread thread3 = new Thread(new Customer(basket));thread1.start();thread2.start();thread3.start();}}class Cake {private static int index = 0;private final int id = index++;public int getId() {return id;}public Cake() {System.out.println("生產了蛋糕,id:" + id);}}class Chief implements Runnable {private Basket basket = null;public Chief(Basket basket) {this.basket = basket;}public void makeCake() throws InterruptedException {for (int i = 0; i < 20; i++) {basket.put(new Cake());System.out.println("現在框裡面的蛋糕數量:" + basket.getQueueSize());Thread.sleep(200);}}@Overridepublic void run() {try {makeCake();} catch (InterruptedException e) {e.printStackTrace();}}}class Customer implements Runnable {private Basket basket = null;public Customer(Basket basket) {this.basket = basket;}public void buyCake() throws InterruptedException {System.out.println("消費了蛋糕,id:" + basket.take().getId());System.out.println("現在框裡面的蛋糕數量:" + basket.getQueueSize());}@Overridepublic void run() {try {for (int i = 0; i < 20; i++) {buyCake();Thread.sleep(100);}} catch (InterruptedException e) {e.printStackTrace();}}}class Basket {private LinkedBlockingQueue queue = new LinkedBlockingQueue(10);public void put(Cake cake) throws InterruptedException {queue.put(cake);}public Cake take() throws InterruptedException {Cake cake = queue.take();return cake;}public int getQueueSize() {return queue.size();}}總結:這一章節我們通過生產者消費者模式來介紹了阻塞隊列的特性。
這一章節就到這裡,謝謝。