ArrayBlockingQueue api详解

今天学习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 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));
        }

}

 

你可能感兴趣的:(jdk源码解析,java,queue,数据结构)