This time I will explain BlockingQueue :
What is BlockingQueue (blocking queue)?
When the queue is empty, getting elements from the queue will block .
When the queue is full, adding elements from the queue will block .
Because it is a queue, we should think of first in first out .
Next we will demonstrate a simple use:
add (throws an exception):
/**
* @author Cocowwy
* @create 2020-05-05-14:53
*/
public class BlockingQueueDemo {
public static void main(String[] args) {
BlockingQueue<String> 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.add("x"));
}
}
The results are as follows:
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 juc.BlockingQueueDemo.main(BlockingQueueDemo.java:16)
We can see that if the added element is greater than 3, an exception will be thrown when the queue is full:java.lang.IllegalStateException: Queue full
remove (throw an exception):
public class BlockingQueueDemo {
public static void main(String[] args) {
BlockingQueue<String> 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.add("x"));
System.out.println(blockingQueue.remove()); //返回值是E(即对象) 因为是队列 所以是a
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
}
}
It should be noted here that the return type of remove is E (object) and the
result is as follows:
true
true
true
a
b
c
Exception in thread "main" java.util.NoSuchElementException
at java.util.AbstractQueue.remove(AbstractQueue.java:117)
at juc.BlockingQueueDemo.main(BlockingQueueDemo.java:20)
Process finished with exit code 1
We can see that if the added element is greater than 3, that is, when the queue is empty and then deleted, an exception will be thrown:java.util.NoSuchElementException
element (throws an exception):
public class BlockingQueueDemo {
public static void main(String[] args) {
BlockingQueue<String> 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());
}
}
The results are as follows:
true
true
true
a
Check what the head element is
offer (special value):
public class BlockingQueueDemo {
public static void main(String[] args) {
BlockingQueue<String> 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"));
}
}
The results are as follows:
true
true
true
false
Insert, return success
pull (special value):
public class BlockingQueueDemo {
public static void main(String[] args) {
BlockingQueue<String> 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());
System.out.println(blockingQueue.poll());
}
}
The results are as follows:
true
true
true
a
b
c
null
peek (special value):
public class BlockingQueueDemo {
public static void main(String[] args) {
BlockingQueue<String> 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());
}
}
The results are as follows:
true
true
true
a
put (blocking):
public class BlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> blockingQueue=new ArrayBlockingQueue<>(3);
blockingQueue.put("a");
blockingQueue.put("b");
blockingQueue.put("c");
blockingQueue.put("d");
}
}
The results are as follows:
Is nothing in the blank, because at this time have been blocked, waiting, and the next take comparison
take (blocking):
public class BlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> blockingQueue=new ArrayBlockingQueue<>(3);
blockingQueue.put("a");
blockingQueue.put("b");
blockingQueue.put("c");
System.out.println(blockingQueue.take());
blockingQueue.put("e");
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
}
}
The results are as follows:
a
b
c
e
Compare with the put above, here if you want to add e after adding abc, it will block, and you can add e after taking out.
offer (timeout):
public class BlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> 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", 3L, TimeUnit.SECONDS));
}
}
The results are as follows:
true
true
true
false //等待了3s
Let it wait 3s, the return value is boolean
poll (timeout):
public class BlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> 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());
System.out.println(blockingQueue.poll(3L, TimeUnit.SECONDS));
}
}
The results are as follows:
true
true
true
a
b
c
null //等待了3s
Based on the above situation, we can get this picture: