BlockingQueue常用方法add、off、put、take、poll使用说明
生活随笔
收集整理的這篇文章主要介紹了
BlockingQueue常用方法add、off、put、take、poll使用说明
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.BlockingQueue的定義
首先將BlockingQueue提供的方法的含義解釋如下:
import java.util.Collection; import java.util.Queue; import java.util.concurrent.TimeUnit;public interface BlockingQueue<E> extends Queue<E> {/*** 1.在不違反容量限制的情況下將指定元素插入隊列,成功則立即返回true。* 2.如果沒有空間則拋出IllegalStateException異常* 3.當使用容量受限時,通常使用offer*/boolean add(E e);/*** 1.在不違反容器限制的情況下降元素插入隊列,成功則返回true* 2.如果沒有可用空間則返回false* 3.當使用容器首先是,此方法比add更可取,因為add需要拋出異常*/boolean offer(E e);/*** 將指定的元素插入隊列,阻塞隊列直到可用*/void put(E e) throws InterruptedException;/*** 將指定的元素插入此隊列,如果需要空間,則等待指定的等待時間。*/boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException;/*** 取出隊列第一個元素并刪除,阻塞隊列直到可用。*/E take() throws InterruptedException;/*** 取出來隊列第一個元素并刪除,可等待指定的等待時間以使元素變為可用。*/E poll(long timeout, TimeUnit unit) throws InterruptedException;/*** 返回此隊列在理想情況下可以不阻塞的接受元素數或者(Integer.MAX_VALUE)如果沒有限制*/int remainingCapacity();/*** 從隊列中刪除指定的元素*/boolean remove(Object o);/*** 如果此隊列包含指定元素則返回true*/public boolean contains(Object o);/*** 從隊列中刪除所有可用元素并將它們添加到指定集合中*/int drainTo(Collection<? super E> c);/*** 從隊列中刪除指定數量的可用元素,并將它們添加到指定集合中*/int drainTo(Collection<? super E> c, int maxElements); }2.測試代碼如下
package com.leo.demo;import org.junit.Test;import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue;/*** @ClassName: BlockingQueueTest* @Description: ${description}* @Author: leo825* @Date: 2020-06-18 21:51* @Version: 1.0*/ public class BlockingQueueTest {BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<String>(3);/*** 將元素插入隊列中,超出容量則拋異常*/@Testpublic void addTest() {boolean result;int queueSize = 3;for (int i = 0; i < queueSize; i++) {String valStr = String.valueOf(i);result = blockingQueue.add(valStr);if (result) {System.out.println("add添加[" + valStr + "]成功了");}}System.out.println(blockingQueue.toString());//添加容量之外的字符串String str0 = "666";result = blockingQueue.add(str0);if (result) {System.out.println("add添加[" + str0 + "]成功了");} else {System.out.println("add添加[" + str0 + "]失敗了");}}/*** 將元素插入隊列中,超出容量不拋異常*/@Testpublic void offerTest() {boolean result;int queueSize = 3;for (int i = 0; i < queueSize; i++) {String valStr = String.valueOf(i);result = blockingQueue.offer(valStr);if (result) {System.out.println("add添加[" + valStr + "]成功了");}}System.out.println(blockingQueue.toString());//添加容量之外的字符串String str0 = "666";result = blockingQueue.offer(str0);if (result) {System.out.println("add添加[" + str0 + "]成功了");} else {System.out.println("add添加[" + str0 + "]失敗了");}}/*** 將元素插入隊列中,超出容量阻塞,常用配合take來做生產者消費者模型*/@Testpublic void putTest() throws InterruptedException {int queueSize = 3;for (int i = 0; i < queueSize; i++) {String valStr = String.valueOf(i);blockingQueue.put(valStr);System.out.println("add添加[" + valStr + "]成功了");}System.out.println(blockingQueue.toString());//添加容量之外的字符串String str0 = "666";blockingQueue.put(str0);System.out.println("add添加[" + str0 + "]成功了");}/*** 獲取隊列的隊頭元素并刪除,如果未獲取到則阻塞隊列直到獲取到,常用配合put來做生產者消費者模型*/@Testpublic void takeTest() throws InterruptedException {int queueSize = 3;for (int i = 0; i < queueSize; i++) {String valStr = String.valueOf(i);blockingQueue.put(valStr);System.out.println("add添加[" + valStr + "]成功了");}System.out.print("take()之前:");System.out.println(blockingQueue.toString());//添加容量之外的字符串String str0;str0 = blockingQueue.take();System.out.println("take獲取[" + str0 + "]成功了");System.out.print("take()之后:");System.out.println(blockingQueue.toString());str0 = blockingQueue.take();System.out.println("take獲取[" + str0 + "]成功了");System.out.print("take()之后:");System.out.println(blockingQueue.toString());str0 = blockingQueue.take();System.out.println("take獲取[" + str0 + "]成功了");System.out.print("take()之后:");System.out.println(blockingQueue.toString());//多取一個則阻塞了str0 = blockingQueue.take();System.out.println("take獲取[" + str0 + "]成功了");System.out.print("take()之后:");System.out.println(blockingQueue.toString());}/*** 獲取隊列的隊頭元素并刪除,如果未獲取到直接返回null*/@Testpublic void poolTest() throws InterruptedException {int queueSize = 3;for (int i = 0; i < queueSize; i++) {String valStr = String.valueOf(i);blockingQueue.put(valStr);System.out.println("add添加[" + valStr + "]成功了");}System.out.print("poll()之前:");System.out.println(blockingQueue.toString());//添加容量之外的字符串String str0;str0 = blockingQueue.poll();System.out.println("poll獲取[" + str0 + "]成功了");System.out.print("poll()之后:");System.out.println(blockingQueue.toString());str0 = blockingQueue.poll();System.out.println("poll獲取[" + str0 + "]成功了");System.out.print("poll()之后:");System.out.println(blockingQueue.toString());str0 = blockingQueue.poll();System.out.println("poll獲取[" + str0 + "]成功了");System.out.print("poll()之后:");System.out.println(blockingQueue.toString());//多取一個則阻塞了str0 = blockingQueue.poll();System.out.println("poll獲取[" + str0 + "]成功了");System.out.print("take()之后:");System.out.println(blockingQueue.toString());} }總結
以上是生活随笔為你收集整理的BlockingQueue常用方法add、off、put、take、poll使用说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机基础——原码、反码、补码转换
- 下一篇: JAVA线程的生命周期以及5种状态转换