BlockingQueue四组API

HeJin大约 3 分钟JavaJUC并发编程

方式抛出异常不会抛出异常,有返回值阻塞一直等待超时等待
添加add(E e)offer(E e)put(E e)offer(, ,)
移除remove()poll()take()pull(,)
检测队首元素elementpeek--

1、抛出异常

add添加元素

/**
 * 抛出异常
 */
public static void test1(){
    // 队列的大小
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

    System.out.println(blockingQueue.add("a"));
    System.out.println(blockingQueue.add("b"));
    System.out.println(blockingQueue.add("c"));
    // 队列已经满了: Queue full异常
    System.out.println(blockingQueue.add("d"));

}

结果:

true
true
true
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 com.hejin.bq.Test.test1(Test.java:27)
	at com.hejin.bq.Test.main(Test.java:13)

Process finished with exit code 1

remove弹出元素

public static void test1(){
    // 队列的大小
    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.remove());
    System.out.println(blockingQueue.remove());
    System.out.println(blockingQueue.remove());
}

结果:

true
true
true
a
b
c

Process finished with exit code 0

队列为空

public static void test1(){
    // 队列的大小
    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.remove());
    System.out.println(blockingQueue.remove());
    System.out.println(blockingQueue.remove());

    // 队列已空:java.util.NoSuchElementException
    System.out.println(blockingQueue.remove());
}

结果:

true
true
true
a
b
c
Exception in thread "main" java.util.NoSuchElementException
	at java.util.AbstractQueue.remove(AbstractQueue.java:117)
	at com.hejin.bq.Test.test1(Test.java:32)
	at com.hejin.bq.Test.main(Test.java:13)

Process finished with exit code 1

element队首元素

public static void test1(){
    // 队列的大小
    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());

    System.out.println(blockingQueue.remove());
    System.out.println("队首:"+blockingQueue.element());
    System.out.println(blockingQueue.remove());
    System.out.println(blockingQueue.remove());

    // 队列已空
    System.out.println("队首:"+blockingQueue.element());
}

结果:

true
true
true
队首:a
a
队首:b
b
c
Exception in thread "main" java.util.NoSuchElementException
	at java.util.AbstractQueue.element(AbstractQueue.java:136)
	at com.hejin.bq.Test.test1(Test.java:35)
	at com.hejin.bq.Test.main(Test.java:13)

Process finished with exit code 1

2、有返回值,不抛出异常

offer添加

/**
 * 有返回值,不抛出异常
 */
public static void test2(){
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

    System.out.println(blockingQueue.offer("a"));
    System.out.println(blockingQueue.offer("b"));
    System.out.println(blockingQueue.offer("c"));
    // 不抛出异常,返回false
    System.out.println(blockingQueue.offer("d"));
}

结果:

true
true
true
false

Process finished with exit code 0

poll弹出

public static void test2(){
    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.poll());
    System.out.println(blockingQueue.poll());
    System.out.println(blockingQueue.poll());
    // 队列已空。返回null
    System.out.println(blockingQueue.poll());
}

结果:

true
true
true
a
b
c
null

Process finished with exit code 0

peek队首元素

public static void test2(){
    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.poll());
    System.out.println("队首:"+blockingQueue.peek());
    System.out.println(blockingQueue.poll());
    System.out.println(blockingQueue.poll());
    // 队列已空。返回null
    System.out.println("队首:"+blockingQueue.peek());
}

结果:

true
true
true
队首:a
a
队首:b
b
c
队首:null

Process finished with exit code 0

3、等待,一直阻塞

put添加元素

public class Test {
    public static void main(String[] args) throws InterruptedException {
        test3();
    }

    /**
     * 等待,阻塞(一直阻塞)
     */
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        // 队列已满。一直等待
        blockingQueue.put("d");
    }
}

结果:程序一直等待。

image-20210312131629258
image-20210312131629258

take取出元素

public static void test3() throws InterruptedException {
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

    blockingQueue.put("a");
    blockingQueue.put("b");
    blockingQueue.put("c");

    System.out.println(blockingQueue.take());
    System.out.println(blockingQueue.take());
    System.out.println(blockingQueue.take());
    // 队列已空:一直等待
    System.out.println(blockingQueue.take());
}

队列中的元素取完之后,一直等待。

image-20210312131853852
image-20210312131853852

4、等待 ,超时退出

offer 超时等待添加元素

/**
 * 等待,阻塞(等待超时)
 */
public static void test4() throws InterruptedException {
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

    blockingQueue.offer("a");
    blockingQueue.offer("b");
    blockingQueue.offer("c");
    // 队列已满。等待2秒,之后超时退出
    blockingQueue.offer("d",2, TimeUnit.SECONDS);
}

等了2秒之后,程序结束。

Process finished with exit code 0

pull 超时等待弹出元素

public static void test4() throws InterruptedException {
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

    blockingQueue.offer("a");
    blockingQueue.offer("b");
    blockingQueue.offer("c");
    // 队列已满。等待2秒,之后超时退出
    blockingQueue.offer("d",2, TimeUnit.SECONDS);
    System.out.println("====================");
    System.out.println(blockingQueue.poll());
    System.out.println(blockingQueue.poll());
    System.out.println(blockingQueue.poll());
    // 队列已空:等待2秒,超时退出
    System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));
}

结果:

====================
a
b
c
null

Process finished with exit code 0