This is the 21st day of my participation in the August Text Challenge.More challenges in August

introduce

Blocking queue can be simply defined as a mailbox with a certain capacity. If the mailbox is full, it will be blocked to put mail into the mailbox, and if the mailbox is empty, it will be blocked to get mail into the mailbox

Blocking queues are often used in producer and consumer scenarios, where the producer is the thread that adds elements to the queue and the consumer is the thread that takes elements from the queue. A blocking queue is a container in which producers hold elements, and consumers only take elements from the container.

  • Fetching an element from the queue is blocked when the element is empty
  • When the queue is full, removing elements from the queue will be blocked

Some blocking queues

  • ArrayBlockingQueue: a bounded blocking queue composed of array structures
  • LinkedBlockingQueue: a bounded blocking queue consisting of a linked list structure (size defaults to integer.max_value)
  • PriorityBlockingQueue:: Unbounded blocking queue that supports priority sorting
  • DelayQueue: delay unbounded blocking queue implemented using priority queues
  • SynchronousQueue: A blocking queue that does not store elements, that is, a queue of individual elements
  • LinkedTransferQueue: An unbounded blocking queue consisting of a linked list structure
  • LinkedBlockingDeque: A two-way blocking queue consisting of a linked list structure
Method type An exception is thrown Special values blocking timeout
insert add(e) offer(e) put(e) offer(e,time,unit)
remove remove() poll() take() poll(time,unit)
check element() peek() Do not use Do not use

What is blocking?

In the case of multithreading, threads are suspended (that is, blocked) because of special circumstances, and they are awakened when certain conditions are met.

What does a blocking queue do?

So we don’t have to worry about when to wake up a thread, or when to block a thread. BlockingQueue takes care of that for us.

Throws the exception type Demo

Add: Throws an exception if the number of elements in the queue is already full

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Code01_BlockingQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(2);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c")); }}Copy the code

Test results:

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.lemon.lesson7_blockingQueue.Code01_BlockingQueueDemo.main(Code01_BlockingQueueDemo.java:11)
Copy the code

Remove: Throws an exception if there are no elements in the queue

public class Code01_BlockingQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(2);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b")); blockingQueue.remove(); blockingQueue.remove(); blockingQueue.remove(); }}Copy the code

Element: Raises an exception if there are no elements in the queue

public class Code01_BlockingQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(2); blockingQueue.element(); }}Copy the code