package com.java.blockingqueue;import org.junit.Test;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.TimeUnit;/**
* @author rrqstart
* @Description 阻塞队列 BlockingQueue
*/publicclassBlockingQueueTest{
@Testpublicvoidtest1(){
//new ArrayBlockingQueue<>(2)等价于new ArrayBlockingQueue<>(2,false)//即不保证线程公平的访问队列。公平性与否使用可重入锁实现。
BlockingQueue<String> blockingQueue =newArrayBlockingQueue<>(2);//new Object[2];//public abstract boolean add(@NotNull E e)
System.out.println(blockingQueue.add("a"));//true
System.out.println(blockingQueue.add("b"));//true// System.out.println(blockingQueue.add("x")); //java.lang.IllegalStateException: Queue full//public abstract E remove()//Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.
System.out.println(blockingQueue.remove());//a
System.out.println(blockingQueue.remove());//b// System.out.println(blockingQueue.remove()); //java.util.NoSuchElementException//public abstract int size()//Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
System.out.println(blockingQueue.size());//0//public abstract E element():检查但不删除此队列的头//Retrieves, but does not remove, the head of this queue. This method differs from pee