If BlockingQueue is full, any operation that attempts to store things in it will be blocked and will not be awakened to continue operations until there is a new space in BlockingQueue.
The main methods provided by BlockingQueue are:
add(anObject): Add anObject to the BlockingQueue. If the BlockingQueue can accommodate the return true, otherwise an IllegalStateException will be thrown.
offer(anObject): Add anObject to the BlockingQueue. If the BlockingQueue can accommodate it, return true, otherwise return false.
put(anObject): Add anObject to the BlockingQueue. If there is no space for BlockingQueue, the thread calling this method is blocked until there is a new space in BlockingQueue before continuing.
poll(time): Take out the object ranked first in BlockingQueue. If it cannot be taken out immediately, you can wait for the time specified by the time parameter. Returns null when it cannot be retrieved.
take(): Take out the object ranked first in BlockingQueue. If BlockingQueue is empty, block the waiting state until a new object in BlockingQueue is added.
There are 4 specific implementations of BlockingQueue according to different needs:
(1) ArrayBlockingQueue: The constructor of the specified size must have an int parameter to indicate its size. The objects it contains are sorted in FIFO (first in, first out).
(2) LinkedBlockingQueue: BlockingQueue with varying size. If its constructor takes a parameter of the specified size, the generated BlockingQueue has a size limit.
If the size parameter is not included, the size of the generated BlockingQueue is determined by Integer.MAX_VALUE. The objects it contains are sorted in FIFO (first-in, first-out) order.
Compared with LinkedBlockingQueue and ArrayBlockingQueue, the data structures used behind them are different.
The data throughput of LinkedBlockingQueue is greater than that of ArrayBlockingQueue, but its performance predictability is lower than that of ArrayBlockingQueue when the number of threads is large.
(3) PriorityBlockingQueue: Similar to LinkedBlockingQueue, but the sorting of objects it contains is not FIFO, but is determined by the natural sorting order of objects or the order determined by the Comparator brought by the constructor.
(4) SynchronousQueue: A special BlockingQueue, the operation of it must be completed alternately by putting and fetching.
The code copy is as follows:
package com.yao;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class BlockingQueueTest {
/**
Defining the basket for Apple
*/
public static class Basket{
// Basket, able to hold 3 apples
BlockingQueue<String> basket = new ArrayBlockingQueue<String>(3);
// Produce apples and put them in a basket
public void produce() throws InterruptedException{
// Put an apple in the put method. If the battery is full, wait until the battery has a location
basket.put("An apple");
}
// Consume apples and take them out of the basket
public String consume() throws InterruptedException{
// Get method takes out an apple. If the battery is empty, wait until there is an apple in the battery.
String apple = basket.take();
return apple;
}
public int getAppleNumber(){
return basket.size();
}
}
// Test method
public static void testBasket() {
// Build a basket with apples
final Basket basket = new Basket();
// Define Apple Producer
class Producer implements Runnable {
public void run() {
try {
while (true) {
// Produce Apple
System.out.println("Producer is ready to produce apples:"
+ System.currentTimeMillis());
basket.produce();
System.out.println("Producer has finished producing apples:"
+ System.currentTimeMillis());
System.out.println("After production, there are apples: "+basket.getAppleNumber()+");
// 300ms sleep
Thread.sleep(300);
}
} catch (InterruptedException ex) {
}
}
}
// Define Apple Consumers
class Consumer implements Runnable {
public void run() {
try {
while (true) {
// Consumption of Apple
System.out.println("Consumers are ready to consume Apple:"
+ System.currentTimeMillis());
basket.consume();
System.out.println("Consumer consumes Apple"
+ System.currentTimeMillis());
System.out.println("After consumption, there are apples: "+basket.getAppleNumber()+");
// 1000ms sleep
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
}
}
}
ExecutorService service = Executors.newCachedThreadPool();
Producer producer = new Producer();
Consumer consumer = new Consumer();
service.submit(producer);
service.submit(consumer);
// After the program runs for 10 seconds, all tasks will be stopped
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
service.shutdownNow();
}
public static void main(String[] args) {
BlockingQueueTest.testBasket();
}
}