Reprint Please specify source:http://blog.csdn.net/ns_code/article/details/17511147
Blocking Queues
The blocking queue is the content in the new Java 5 concurrency feature, and the interface for the blocking queue is Java.util.concurrent.BlockingQueue, which has multiple implementation classes: Arrayblockingqueue, Delayqueue, Linkedblockingqueue, Priorityblockingqueue, Synchronousqueue, etc., the use of a similar, specific to view the JDK documentation, here is a simple example of arrayblockingqueue, It implements a bounded queue, and when the queue is full, it blocks the wait until there are elements out of the queue and subsequent elements can be queued.
Look at the following example:
[Java]View PlainCopy
- Import Java.util.concurrent.BlockingQueue;
- Import Java.util.concurrent.ArrayBlockingQueue;
- Public class blockingqueuetest{
- public static void Main (string[] args) throws interruptedexception {
- blockingqueue<string> bqueue = new arrayblockingqueue<string> (20);
- For (int i = 0; i < ; i++) {
- //Add the specified element to this queue
- Bqueue.put ("add element" + i);
- System.out.println ("element added to the blocking queue:" + i);
- }
- SYSTEM.OUT.PRINTLN ("program to this run end, will exit----");
- }
- }
The output results are as follows:
As you can see from the execution results, because the number of elements in the queue is limited to 20, after adding 20 elements, the other elements block the wait outside the queue, and the program does not terminate.
If the queue is full, we move the first element out of the team and can continue to add elements to the blocking queue, modifying the code as follows:
[Java]View PlainCopy
- Import Java.util.concurrent.BlockingQueue;
- Import Java.util.concurrent.ArrayBlockingQueue;
- Public class blockingqueuetest{
- public static void Main (string[] args) throws interruptedexception {
- blockingqueue<string> bqueue = new arrayblockingqueue<string> (20);
- For (int i = 0; i < ; i++) {
- //Add the specified element to this queue
- Bqueue.put ("" + i);
- System.out.println ("element added to the blocking queue:" + i);
- if (i > ) {
- //Get the team head element from the queue and move it out of the queue
- System.out.println ("remove elements from the blocking queue:" + bqueue.take ());
- }
- }
- SYSTEM.OUT.PRINTLN ("program to this run end, will exit----");
- }
- }
The results of the implementation are as follows:as you can see from the results, when the 20th element is added, we move the element from the first line, so that we can continue to add elements to the queue, and then each time we add an element, we remove the first element from the team, so that the program can execute the end.
Blocking Stacks
The blocking stack is similar to the blocking queue, except that it is a new feature added in Java 6, the interface java.util.concurrent.BlockingDeque of the blocking stack is also a lot of implementation classes, the use of similar methods, the specific view of the JDK document.
A simple example is also given below:
[Java]View PlainCopy
- Import Java.util.concurrent.BlockingDeque;
- Import Java.util.concurrent.LinkedBlockingDeque;
- Public class Blockingdequetest {
- public static void Main (string[] args) throws interruptedexception {
- blockingdeque<string> bdeque = new linkedblockingdeque<string> (20);
- For (int i = 0; i < ; i++) {
- //Add the specified element to this blocking stack
- Bdeque.putfirst ("" + i);
- System.out.println ("added elements to the blocking stack:" + i);
- }
- SYSTEM.OUT.PRINTLN ("program to this run end, will exit----");
- }
- }
The results of the implementation are as follows:
The program will still block the wait, let's change to the following code:
[Java]View PlainCopy
- Import Java.util.concurrent.BlockingDeque;
- Import Java.util.concurrent.LinkedBlockingDeque;
- Public class Blockingdequetest {
- public static void Main (string[] args) throws interruptedexception {
- blockingdeque<string> bdeque = new linkedblockingdeque<string> (20);
- For (int i = 0; i < ; i++) {
- //Add the specified element to this blocking stack
- Bdeque.putfirst ("" + i);
- System.out.println ("added elements to the blocking stack:" + i);
- if (i > ) {
- //Remove the top element of the stack from the blocking stack and move it out
- System.out.println ("removed elements from the blocking stack:" + bdeque.pollfirst ());
- }
- }
- SYSTEM.OUT.PRINTLN ("program to this run end, will exit----");
- }
- }
The results of the implementation are as follows:
as can be seen from the results, when the 20th element is added, we move from the top of the stack, so that we can continue to add elements to the stack, then each element added, the top element of the stack is moved out, so that the program can execute the end.
Turn: Java concurrency Programming 21: Concurrency new features-blocking queues and blocking stacks (with code)