阻塞队列
一、阻塞队列(BlockingQueue )
四种操作方式
方式 | 抛出异常 | 不抛出异常,有返回值 | 阻塞等待 | 超时等待 |
---|---|---|---|---|
添加 | add() | offer() | put() | offer(E e,long timeout, TimeUnit unit)重载方法 |
移除 | remove() | poll() | take() | poll(long timeout, TimeUnit unit)重载方法 |
检测队首元素 | element() | peek() | 无 | 无 |
public class BlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
test4();
}
//抛出异常
public static void test1(){
BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
System.out.println(blockingQueue.add("a"));
System.out.println(blockingQueue.add("b"));
System.out.println(blockingQueue.add("c"));
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
}
//有返回值
public static void test2(){
BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
System.out.println(blockingQueue.offer("a"));
System.out.println(blockingQueue.offer("b"));
System.out.println(blockingQueue.offer("c"));
System.out.println(blockingQueue.offer("d"));
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
}
//阻塞等待
public static void test3() throws InterruptedException {
BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
blockingQueue.put("a");
blockingQueue.put("b");
blockingQueue.put("c");
blockingQueue.put("d"); //会一直等
}
//超时等待
public static void test4() throws InterruptedException {
BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
System.out.println(blockingQueue.offer("a"));
System.out.println(blockingQueue.offer("b"));
System.out.println(blockingQueue.offer("c"));
System.out.println(blockingQueue.offer("d",2, TimeUnit.SECONDS));
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));
}
}
运行结果
1、抛出异常
Exception in thread "main" java.lang.IllegalStateException: Queue full
at java.util.AbstractQueue.add(AbstractQueue.java:98)
at java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:312)
at gdut.hzh.blockingqueue.BlockingQueueDemo.test1(BlockingQueueDemo.java:21)
at gdut.hzh.blockingqueue.BlockingQueueDemo.main(BlockingQueueDemo.java:13)
Exception in thread "main" java.util.NoSuchElementException
at java.util.AbstractQueue.remove(AbstractQueue.java:117)
at gdut.hzh.blockingqueue.BlockingQueueDemo.test1(BlockingQueueDemo.java:24)
at gdut.hzh.blockingqueue.BlockingQueueDemo.main(BlockingQueueDemo.java:13)
2、有返回值
true
true
true
false
a
b
c
null
3、阻塞等待
4、超时等待
true
true
true
压入第四个,超时等待2s
false
a
b
c
弹出第四个,超时等待2s
null
二、同步队列(SynchronousQueue)
没有容量,进去一个元素,必须等待取出来之后,才能往里面放一个元素
放进去:put,取出来:take
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();
}
}
运行结果:
T1 put 1
T2==>1
T1 put 2
T2==>2
T1 put 3
T2==>3