`ArrayBlockingQueue` 是一个先进先出的有界队列,初始化容量后,无法进行修改
api
add
超出队列会报错 java.lang.IllegalStateException: Queue full (对应的remove 方法)
offer
添加返回true 和 false
put
会阻塞 (对应的take)
poll
拿不出来,返回null
peek
只拿出第一位,不会删除
element
只拿出第一位,不会删除.但是队列清空会报错 , 但是peek返回null
remove
队列里没有元素,就抛出异常
DrainTo
把队列排干到集合里面
private static ArrayBlockingQueue<String> queue;
/**
* FIFO(first in first out)
* Once created , the capacity cannot be changed
* @param size
* @param <T>
* @return
*/
public <T>ArrayBlockingQueue<T> create(int size){
return new ArrayBlockingQueue<>(size);
}
/**
* add 超出队列会报错 java.lang.IllegalStateException: Queue full
*/
public static void testAdd(){
ArrayBlockingQueueExample example = new ArrayBlockingQueueExample();
queue = example.create(5);
queue.add("1");
queue.add("1");
queue.add("1");
queue.add("1");
/* queue.add("1");
queue.add("1");*/
System.out.println(queue.size());
}
/**
* offer 添加返回true 和 false
*/
public static void testOffer(){
ArrayBlockingQueueExample example = new ArrayBlockingQueueExample();
queue = example.create(5);
queue.offer("1");
queue.offer("1");
queue.offer("1");
queue.offer("1");
/*System.out.println(queue.offer("1"));
System.out.println(queue.offer("1"));*/
System.out.println(queue.size());
}
/**
* put 会阻塞
* @throws InterruptedException
*/
public static void testPut() throws InterruptedException {
ArrayBlockingQueueExample example = new ArrayBlockingQueueExample();
queue = example.create(5);
queue.put("1");
queue.put("1");
queue.put("1");
queue.put("1");
queue.put("1");
queue.put("1");
System.out.println(queue.size());
}
/**
* 拿不出来,返回null
*/
public static void testPoll(){
queue.poll();
queue.poll();
queue.poll();
queue.poll();
System.out.println(queue.poll());
}
/**
* 只拿出第一位,不会删除
*/
public static void testPeek(){
System.out.println(queue.peek());
System.out.println(queue.peek());
System.out.println(queue.peek());
queue.clear();
System.out.println(queue.peek() + "----");
}
/**
* 只拿出第一位,不会删除
* 但是队列清空会报错 , 但是peek返回null
*/
public static void testElement(){
System.out.println(queue.element());
System.out.println(queue.element());
System.out.println(queue.element());
queue.clear();
System.out.println(queue.size());
queue.element();
}
/**
* 队列里没有元素,就抛出异常
*
*/
public static void testRemove(){
queue.remove();
queue.remove();
queue.remove();
System.out.println(queue.size());
queue.remove();
}
/**
* take 和put 对应 会阻塞
* @throws InterruptedException
*/
public static void testTake() throws InterruptedException {
queue.take();
queue.take();
queue.take();
System.out.println(queue.size());
queue.take();
queue.take();
}
/**
* 会把当前的队列排干掉
*/
public static void testDrainTo(){
List<String> ls = new ArrayList<>();
queue.drainTo(ls);
System.out.println(ls.size());
System.out.println(queue.size());
}
public static void main(String[] args) throws InterruptedException {
testAdd();
// testOffer();
// testPut();
// testPoll();
// testPeek();
// testElement();
// testRemove();
// testTake();
testDrainTo();
}
PriorityBlockingQueue
无边界队列,
如果添加是对象要实现comparable 接口 或者函数式编程接口
其他和ArrayBlockingQueue基本相似
private static PriorityBlockingQueue queue;
public static void create(){
/**
* 是一个无边界的 ,这个设置是初始化值
*/
queue = new PriorityBlockingQueue(2);
}
/*
* add 无边界证明
* 如果是对象要实现comparable 接口 或者函数式编程接口
*/
public static void testAdd(){
queue.add("1");
queue.add("3");
queue.add("2");
}
// 最先拿出来的是1 , element 不会把数据剔除队列
public static void testElement(){
System.out.println(queue.element());
}
// 同 element
public static void testPeek(){
System.out.println(queue.peek());
System.out.println(queue.peek());
System.out.println(queue.peek());
}
// 如果没有值弹出去,则返回null
public static void testPoll(){
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
}
// 如果没有值弹出去,则抛错
public static void tesRemove(){
System.out.println(queue.remove());
System.out.println(queue.remove());
System.out.println(queue.remove());
System.out.println(queue.remove());
}
// 没有值会阻塞
public static void testTake() throws InterruptedException {
System.out.println(queue.take());
}
public static void main(String[] args) {
create();
testAdd();
// testElement();
// testPeek();
// testPoll();
tesRemove();
}
LinkedBlockingQueue
api 和上面两个的效果都一致
LinkedBlockingQueue是一个单向链表实现的阻塞队列。该队列按 FIFO(先进先出)排序元素,新元素插入到队列的尾部,并且队列获取操作会获得位于队列头部的元素。链接队列的吞吐量通常要高于基于数组的队列,但是在大多数并发应用程序中,其可预知的性能要低。
此外,LinkedBlockingQueue还是可选容量的(防止过度膨胀),即可以指定队列的容量。如果不指定,默认容量大小等于Integer.MAX_VALUE。
作者:AlstonWilliams
链接:https://www.jianshu.com/p/9394b257fdde
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。