今天學習ArrayBlockingQueue,該類是JUC原子包中的類,通過單元測試代碼把所有public api方法跑了一遍,大致瞭解了底層實現,初學乍練,有很多一知半解的地方,待後續有了深入理解再來補充
package test.java.util.concurrent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.junit.Test;
/**
* ArrayBlockingQueue的測試類
*
* @author zqw
* @date 2020-06-28 22:24:43
*/
public class ArrayBlockingQueueTest {
/**
* 初始化數組隊列
* void
* @Param 數組大小
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testConstruct0()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
System.out.println(testObj.size());
}
/**
* 初始化數組隊列
* void
* @Param 數組大小
* 是否爲公平鎖(詳情見ReentrantLock)
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testConstruct1()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33,true);
System.out.println(testObj.size());
}
/**
* 初始化數組隊列
* void
* @Param 數組大小
* 是否爲公平鎖(詳情見ReentrantLock)
* 要添加的元素
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testConstruct2()throws Exception{
List<Integer> list=new ArrayList<>();
list.add(3);
list.add(4);
list.add(5);
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33,true,list);
System.out.println(testObj.size());
System.out.println(testObj.take());
}
/**
* 添加元素
* add和offer在arrayBlockingQueue中效果一樣
* void
* @Param
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testAdd()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.add(2);
System.out.println(testObj.take());
}
/**
* offer添加,首先通過lock獲取同步鎖,然後檢查
* 隊列是否已滿,滿了返回false不添加,不滿則添加
* void
* @Param
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testOffer1()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.offer(2);
System.out.println(testObj.take());
}
/**
* 添加元素,首先通過lock獲取同步鎖,然後檢查
* 隊列是否已滿,滿了則使用Condition.await等待,不滿則添加
* void
* @Param
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testPut()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.take());
}
/**
* 添加元素,首先通過lock獲取同步鎖,然後檢查
* 隊列是否已滿,滿了則使用Condition.await等待第二個參數的時間,如果時間
* 到了還是滿的,則返回false,不滿則添加
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testOffer2()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.offer(2,2000, TimeUnit.SECONDS);
System.out.println(testObj.take());
}
/**
* 首先通過lock獲取同步鎖
* 彈出元素
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testPoll1()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.poll());
}
/**
首先通過lock獲取同步鎖lockInterruptibly
* 彈出元素,如果爲空,則等待,知道有元素爲止
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testTake()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.take());
}
/**
* 首先通過lock獲取同步鎖
* 如果爲空則等待參數時間,否則彈出元素
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testPoll()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.poll(2000,TimeUnit.SECONDS));
}
/**
*彈出隊列下一個元素,即takeIndex下標的元素
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testPeek()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.peek());
}
/**
* 數組中元素個數
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testSize()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.size());
}
/**
* 數組隊列剩餘大小
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testRemainingCapacity()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.remainingCapacity());
}
/**
* 移除對應元素
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testRemove()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.size());
System.out.println(testObj.remove(2));
System.out.println(testObj.size());
}
/**
* 是否包含對應元素
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testContains()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.contains(2));
}
/**
*轉換爲數組,使用System.arraycopy
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testToArray1()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.toArray());
}
/**
* 將元素拷貝到指定數組,如果指定數組大小不夠,則先
* 擴容
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testToArray()throws Exception{
Object[] bb=new Object[3];
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.toArray(bb));
}
/**
* 轉換爲字符串
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testToString()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.toString());
}
/**
* 清空隊列
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testClear()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
testObj.clear();
System.out.println(testObj.size());
System.out.println(testObj.poll());
}
/**
*將隊列中元素一次性拷貝到指定集合中,並返回大小
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testDrainTo1()throws Exception{
List d=new ArrayList();
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
System.out.println(testObj.drainTo(d));
System.out.println(d.get(0));
}
/**
*將隊列中元素一次性拷貝到指定集合中,並返回大小
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testDrainTo()throws Exception{
List d=new ArrayList();
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
testObj.put(2);
testObj.put(2);
testObj.put(2);
testObj.put(2);
testObj.put(2);
System.out.println(testObj.drainTo(d,3));
System.out.println(d.size());
System.out.println(d.get(0));
}
/**
* 通過迭代器遍歷
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testIterator()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
Iterator iterator=testObj.iterator();
while (iterator.hasNext())
System.out.println(iterator.next());
}
/**
* 並行迭代器,可使用Consumer 並行消費隊列中元素
* @author zhqwm
* @date 2020/6/28 23:01
*/
@Test
public void testSpliterator()throws Exception{
ArrayBlockingQueue testObj=new ArrayBlockingQueue(33);
testObj.put(2);
Spliterator spliterator=testObj.spliterator();
Consumer consumer=new Consumer() {
@Override
public void accept(Object o) {
System.out.println(o);
}
};
boolean dd=spliterator.tryAdvance(consumer);
System.out.println(dd);
System.out.println(spliterator.tryAdvance(consumer));
}
}