常见队列家族
使用阻塞队列的情况:多线程并发处理,线程池
阻塞队列的四种API:
方式 | 抛出异常 | 有返回值,不抛出异常 | 阻塞 等待 | 超时等待 |
---|---|---|---|---|
添加 | add() | offer() | put() | offer(,) |
移除 | remove() | poll() | take() | poll(,) |
判断队列首 | element | peek | - | - |
public class Test {
public static void main(String[] args) throws InterruptedException {
//test01();
// test02();
// test03();
test04();
}
/*
抛出异常
*/
public static void test01(){
//设置队列大小
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.add("a"));//true
System.out.println(blockingQueue.add("b"));//true
System.out.println(blockingQueue.add("c"));//true
//队列满了再添加会抛出异常 IllegalStateException: Queue full
//System.out.println(blockingQueue.add("d"));
//查看队首元素是谁
System.out.println(blockingQueue.element());
System.out.println(blockingQueue.remove());//a
System.out.println(blockingQueue.remove());//b
//弹出两个后再查看队首元素是谁
System.out.println(blockingQueue.element());
System.out.println(blockingQueue.remove());//c
//队列空了再取就会抛出异常 NoSuchElementException
//System.out.println(blockingQueue.remove());
}
/*
有返回值,不抛出异常
*/
public static void test02(){
//设置队列大小
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.offer("a"));//true
System.out.println(blockingQueue.offer("b"));//true
System.out.println(blockingQueue.offer("c"));//true
//查看队首元素是谁
System.out.println(blockingQueue.peek());
//队列满了再添加会返回false,不会抛出异常
System.out.println(blockingQueue.offer("d"));//false
System.out.println(blockingQueue.poll());//a
System.out.println(blockingQueue.poll());//b
//弹出两个后再查看队首元素是谁
System.out.println(blockingQueue.peek());
System.out.println(blockingQueue.poll());//c
//队列空了再取就会返回null,不会抛出异常
System.out.println(blockingQueue.poll());//null
}
/*
等待,阻塞(一直阻塞)
*/
public static void test03() throws InterruptedException {
//设置队列大小
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
blockingQueue.put("a");
blockingQueue.put("b");
blockingQueue.put("c");
// blockingQueue.put("d");//队列没有位置,会一直阻塞
System.out.println(blockingQueue.take());//a
System.out.println(blockingQueue.take());//b
System.out.println(blockingQueue.take());//c
// System.out.println(blockingQueue.take());//队列空了,会一直阻塞
}
/*
等待,阻塞(等待超时)
*/
public static void test04() throws InterruptedException {
//设置队列大小
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.offer("a"));//true
System.out.println(blockingQueue.offer("b"));//true
System.out.println(blockingQueue.offer("c"));//true
//队列满了再添加先等2S,如果超过2S还没有位置就退出或执行下面的代码
blockingQueue.offer("d",2, TimeUnit.SECONDS);
//查看队首元素是谁
System.out.println(blockingQueue.peek());
System.out.println(blockingQueue.poll());//a
System.out.println(blockingQueue.poll());//b
System.out.println(blockingQueue.poll());//c
//队列空了再取最多等2S,超过2S没有就退出或执行下面的代码
blockingQueue.poll(2,TimeUnit.SECONDS);
System.out.println("==============");
}
}
SynchronousQueue同步队列
和其他BlockingQueue不一样,SynchronousQueue不存储元素
put一个元素,必须从里面先take出来,否则不能put进去下一个
可以理解为队列长度为1的特殊BlockingQueue
public class SynchronousQueueDemo {
public static void main(String[] args) {
BlockingQueue<String> blockingQueue = new SynchronousQueue<>();
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+"put 1");
blockingQueue.put("1");
System.out.println(Thread.currentThread().getName()+"put 2");
blockingQueue.put("2");
System.out.println(Thread.currentThread().getName()+"put 3");
blockingQueue.put("3");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"T1").start();
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
},"T2").start();
}
}