阻塞队列
![]()
四组API
| 方式 |
抛出异常 |
有返回值 |
阻塞 等待 |
超时等待 |
| 添加 |
add |
offer |
put |
offer(元素,等待时间,等待时间单位) |
| 移除 |
remove |
poll |
take |
poll(元素,等待时间,等待时间单位) |
| 过去队列首 |
element |
peek |
|
|
public class Test {
public static void main(String[] args) throws InterruptedException {
test4();
}
/*
* 1.抛出异常
* */
public static void test1() {
//队列大小
BlockingQueue queue = new ArrayBlockingQueue(3);
//添加
System.out.println(queue.add("a"));//true
System.out.println(queue.add("b"));//true
System.out.println(queue.add("c"));//true
//System.out.println(queue.add("d"));//IllegalStateException Queue full 队列已满
System.out.println(queue.element());//a 首元素
//移除
System.out.println(queue.remove());//a
System.out.println(queue.remove());//b
System.out.println(queue.remove());//c
System.out.println(queue.remove());//NoSuchElementException 没有元素了
}
/*
* 2.抛出异常
* */
public static void test2() {
//队列大小
BlockingQueue queue = new ArrayBlockingQueue(3);
//添加
System.out.println(queue.offer("a"));//true
System.out.println(queue.offer("b"));//true
System.out.println(queue.offer("c"));//true
System.out.println(queue.offer("d"));//false
System.out.println(queue.peek());
//移除
System.out.println(queue.poll());//a
System.out.println(queue.poll());//b
System.out.println(queue.poll());//c
System.out.println(queue.poll());
}
/*
* 3.阻塞,等待
* */
public static void test3() throws InterruptedException {
BlockingQueue queue = new ArrayBlockingQueue<>(3);
//添加
queue.put("a");
queue.put("b");
queue.put("c");
//queue.put("d");//队列没有位置了,一直阻塞,等待
//移除
System.out.println(queue.take());
System.out.println(queue.take());
System.out.println(queue.take());
System.out.println(queue.take());//一直阻塞,等待
}
/*
* 4.等待 超时
* */
public static void test4() throws InterruptedException {
BlockingQueue queue=new ArrayBlockingQueue(3);
System.out.println(queue.offer("a"));
System.out.println(queue.offer("b"));
System.out.println(queue.offer("c"));
System.out.println(queue.offer("d",2, TimeUnit.SECONDS));//等待2秒后退出,false
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll(2,TimeUnit.SECONDS));//等待2秒后,null
}
}
SynchronousQueue
/*
* 同步队列
* 和其他BlockingQueue不一样,SynchronousQueue不能存贮元素,
* put一个元素,必须先take一个元素
* */
public class SynchronousQueueTest {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> queue = new SynchronousQueue<>();
new Thread(() -> {
try {
System.out.println(Thread.currentThread().getName() + " put a");
queue.put("a");
System.out.println("queue:"+queue);
System.out.println(Thread.currentThread().getName() + " put b");
queue.put("b");
System.out.println(Thread.currentThread().getName() + " put c");
queue.put("c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "t1").start();
/*new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName() + " take " + queue.take());
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName() + " take " + queue.take());
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName() + " take " + queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "t2").start();*/
}
}