/**
This example introduces a special queue: blockingqueue. If blockqueue is empty, the operations to get things from blockingqueue will be blocked and enter the waiting state, it will not be awakened until blockingqueue has entered something. similarly, if blockingqueue is full, any operation that tries to store things in will be blocked and enters the waiting state until blockingqueue has space to wake up and continue the operation.
In this example, we implement the Basket program described in The 11.4 thread-condition. However, the number of the most versatile apples in this basket is not 1 and can be specified at will. when the basket is full, the producer enters the waiting status. When the basket is empty, the consumer waits.
*/
/**
Key Technical Points for using blockingqueue are as follows:
1. blockingqueue:
1) Add (anobject): Add anobject to blockingqueue. If blockingqueue can be accommodated, true is returned. Otherwise, a recruitment exception occurs.
2) offer (anobject): if possible, add anobject to blockingqueue. That is, if blockingqueue can be accommodated, true is returned; otherwise, false is returned.
3) Put (anobject): Add anobject to blockingqueue. If blockqueue has no space, the thread that calls this method is blocked until there is space in blockingqueue to continue.
4) poll (time): removes the first object in blockingqueue. If it cannot be retrieved immediately, it can wait for the time specified by the time parameter. If it cannot be obtained, null is returned.
5) Take (): removes the first object in blockingqueue. If blockingqueue is empty, the block enters the waiting state until a new object is added to blocking.
2. blockingqueue has four specific implementation classes. Select different implementation classes based on different requirements.
1) arrayblockingqueue: Specifies the size of blockingqueue. Its constructor must include an int parameter to specify its size. Its contained objects are sorted in FIFO (first in first out) Order.
2) linkedblockingqueue: A blockingqueue of an indefinite size. If its constructor carries a specified size parameter, the size of the generated blockingqueue is limited. If no size parameter is provided, the size of the generated blockingqueue is determined by integer. max_value. the objects contained in the table are sorted in FIFO (first-in-first-out) Order.
3) priorityblockingqueue: similar to sort blockqueue, but the sorting of objects contained in it is determined by the natural sorting order of objects or the comparator of the constructor.
4) synchronousqueue: Special blockingqueue. The operations on this queue must be completed in turn by putting and fetching.
3. Compared with arrayblockingqueue, the data structures behind them are different. As a result, the data throughput of linkedblockingqueue is larger than that of arrayblockingqueue. However, when the number of threads is large, the performance of linkedblockingqueue is lower than that of arrayblock.
*/