java基础3
阻塞队列ArrayBlockingQueue
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FuvttpWS-1612011642946)(C:\Users\万年历1\AppData\Roaming\Typora\typora-user-images\image-20210130205924372.png)]
四组API
阻塞队列API特性 | 入队 | 出队 | 头元素 |
---|---|---|---|
不抛出异常 | offer | poll | peek |
阻塞等待 | put | take | – |
超时异常 | offer(多参数) | poll(含参数) | – |
抛出异常 | add | remove | element |
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
public class TestQueue {
public static void main(String[] args) throws InterruptedException {
test1();
System.out.println("===================");
test2();
System.out.println("===================");
test3();
System.out.println("===================");
test4();
}
public static void test1(){
ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue(3);
System.out.println(blockingQueue.add('a'));
System.out.println(blockingQueue.add('c'));
System.out.println(blockingQueue.add('d'));
//System.out.println(blockingQueue.add('e'));
System.out.println(blockingQueue.element());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
//System.out.println(blockingQueue.remove());
}
public static void test2(){
ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue(3);
System.out.println(blockingQueue.offer('a'));
System.out.println(blockingQueue.offer('c'));
System.out.println(blockingQueue.offer('d'));
System.out.println(blockingQueue.offer('e'));
System.out.println(blockingQueue.peek());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.peek());
//System.out.println(blockingQueue.remove());
}
//等待,然后一直阻塞。
public static void test3() throws InterruptedException {
ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue(3);
blockingQueue.put('a');
blockingQueue.put('c');
blockingQueue.put('d');
//一直阻塞,队列没有位子了。
//blockingQueue.put('e');
System.out.println(blockingQueue.peek());
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
//System.out.println(blockingQueue.take());
//System.out.println(blockingQueue.remove());
}
public static void test4() throws InterruptedException {
ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue(3);
System.out.println(blockingQueue.offer('a'));
System.out.println(blockingQueue.offer('c'));
System.out.println(blockingQueue.offer('d'));
//队列阻塞,等待两秒后就停止写入。
System.out.println(blockingQueue.offer('e',2,TimeUnit.SECONDS));
System.out.println(blockingQueue.peek());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
//读取数据,等待超过两秒就退出。
System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));
System.out.println(blockingQueue.peek());
//System.out.println(blockingQueue.remove());
}
}
运行结果:
true
true
true
a
a
c
d
===================
true
true
true
false
a
a
c
d
null
===================
a
a
c
d
===================
true
true
true
false
a
a
c
d
null