BlockingQueue阻塞队列
BlockingQueue的四组API
/**BlockQueue的四组API
* 1.抛出异常
* 2.有返回值,不抛出异常
* 3.阻塞等待
* 4.超时等待
*/
public class BlockQueueTest {
public static void main(String[] args) throws InterruptedException {
test03();
}
/**抛出异常
* */
public static void test01(){
ArrayBlockingQueue 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.element());
//IllegalStateException: Queue full 抛出异常,列满
//blockingQueue.add("d");
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
//.NoSuchElementException 队空
//System.out.println(blockingQueue.remove());
}
/**不抛出异常,有返回值
* */
public static void test02(){
ArrayBlockingQueue 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.peek());
//有返回值,不抛出异常
// 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 test03() throws InterruptedException {
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);
arrayBlockingQueue.put("a");
arrayBlockingQueue.put("b");
arrayBlockingQueue.put("c");
//队满,阻塞等待,一直等待
//arrayBlockingQueue.put("d");
System.out.println(arrayBlockingQueue.take());
System.out.println(arrayBlockingQueue.take());
System.out.println(arrayBlockingQueue.take());
//队空,阻塞等待,一直等待
//System.out.println(arrayBlockingQueue.take());
}
/**
*超时等待,但不会一直等待,阻塞时间内等待,之后退出
*/
public static void test04() throws InterruptedException {
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.offer("a", 2, TimeUnit.SECONDS));
System.out.println(blockingQueue.offer("b", 2, TimeUnit.SECONDS));
System.out.println(blockingQueue.offer("c", 2, TimeUnit.SECONDS));
//阻塞两秒,队满返回false退出
// System.out.println(blockingQueue.offer("c", 2, TimeUnit.SECONDS));
System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
//阻塞异常,阻塞两秒,队空返回null
// System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
}
}
SynchronousQueue同步队列
特点:
-
插入操作必须等待队列中的上一个元素被取出后才能插入,反之亦然。
-
同步队列没有存储空间
-
存储元素操作put() ,读取操作take( )
//同步队列
public class SynchronousQueueTest {
public static void main(String[] args) {
//SynchronousQueue是BlockingQueue的子类
BlockingQueue<String> synchronousQueue = new SynchronousQueue<>();
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+" put1");
synchronousQueue.put("a");
System.out.println(Thread.currentThread().getName()+" put2");
synchronousQueue.put("b");
System.out.println(Thread.currentThread().getName()+" put3");
synchronousQueue.put("c");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName()+"----"+synchronousQueue.take());
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName()+"----"+synchronousQueue.take());
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName()+"----"+synchronousQueue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
},"B").start();
}
}
标签:队列,System,阻塞,SECONDS,println,TimeUnit,blockingQueue,BlockingQueue,out
From: https://www.cnblogs.com/wfy-studying/p/16811703.html