The blocking queue is the content in the assigns feature of the Java5 line, and Java defines the interface java.util.concurrent.BlockingQueue that blocks the queue, and the concept of a blocking queue is a queue of a specified length, if the queue is full, The action to add a new element is blocked waiting until there is a vacancy. Similarly, when the queue is empty, the operation of the request queue element also blocks the wait until there are available elements.
With this feature, it is useful to open up a convenient channel for multi-threaded queued model implementations.
Java.util.concurrent.BlockingQueue inherits the Java.util.Queue interface and can refer to the API documentation.
Package Cn.thread;import Java.util.concurrent.arrayblockingqueue;import java.util.concurrent.blockingqueue;/** * Block Queue * * @author Lin Yi-chin * @version 1.0 2013-7-25 pm 04:59:02 */public class Arrayblockingqueuetest {public static Voi D main (string[] args) throws interruptedexception { Blockingqueue queue = new Arrayblockingqueue; for (int i = 0; i <; i++) { //adds the specified element to this queue, if there is no free space, waits (if necessary). queue.put (i); System.out.println ("element added to the blocking queue:" + i); } System.out.println ("program runs to this end, will exit----");} }
Added elements to the blocking queue: 0 added elements to the blocking queue: 1 Adds an element to the blocking queue: 2 Adds an element to the blocking queue: 3 Adds an element to the blocking queue: 4 Adds an element to the blocking queue: 5 Adds an element to the blocking queue: 6 Adds an element to the blocking queue : 7 Adds an element to the blocking queue: 8 Adds an element to the blocking queue: 9 Adds an element to the blocking queue: 10 adds an element to the blocking queue: 11 adds an element to the blocking queue: 12 adds an element to the blocking queue: 13 adds an element to the blocking queue: 14 Adds an element to the blocking queue : 15 adds an element to the blocking queue: 16 Adds an element to the blocking queue: 17 Adds an element to the blocking queue: 18 adds an element to the blocking queue: 19
As you can see, the output to element 19 is always waiting, because the queue is full and the program is blocked.
Java multithreading-new features-blocking queue Arrayblockingqueue