Blocking queue
Blocking queue is the content of Java 5 concurrency new features, and the interface for blocking queues is java. util. concurrent. blockingQueue, which has multiple implementation classes: ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue, PriorityBlockingQueue, and SynchronousQueue. The usage is similar. For details, refer to the JDK documentation. Here is a simple example, it implements a bounded queue. When the queue is full, the wait will be blocked until an element leaves the queue and subsequent elements can be added to the queue.
See the following example:
Import java. util. concurrent. BlockingQueue; import java. util. concurrent. ArrayBlockingQueue; public class BlockingQueueTest {public static void main (String [] args) throws InterruptedException {BlockingQueue
Bqueue = new ArrayBlockingQueue
(20); for (int I = 0; I <30; I ++) {// Add the specified element to this queue bqueue. put (add element + I); System. out. println (added element to the blocking queue: + I);} System. out. println (the program ends now and is about to exit ----);}}
The output result is as follows:
As can be seen from the execution results, because the number of elements in the queue is limited to 20, after adding 20 elements, other elements will block the waiting outside the queue, and the program has not ended.
If the queue is full, we can remove the first element from the queue and add the element to the blocked queue. The Code is as follows:
Import java. util. concurrent. BlockingQueue; import java. util. concurrent. ArrayBlockingQueue; public class BlockingQueueTest {public static void main (String [] args) throws InterruptedException {BlockingQueue
Bqueue = new ArrayBlockingQueue
(20); for (int I = 0; I <30; I ++) {// Add the specified element to this queue bqueue. put (+ I); System. out. println (added element to the blocking queue: + I); if (I> 18) {// gets the element from the queue header and removes it from the queue System. out. println (remove element from the blocking queue: + bqueue. take () ;}} System. out. println (the program ends now and is about to exit ----);}}
The execution result is as follows:
From the results, we can see that after 20th elements are added, we remove an element from the first of the queue, so that we can continue to add elements to the queue, and then add each element, remove the first element from the team so that the program can be executed.
Stack Blocking
The blocking stack is similar to the blocking queue, but it is a new feature added to Java 6, blocking the stack interface java. util. concurrent. blockingDeque also has many implementation classes and uses similar methods. For details, see the JDK documentation.
The following is a simple example:
Import java. util. concurrent. BlockingDeque; import java. util. concurrent. Blocked BlockingDeque; public class BlockingDequeTest {public static void main (String [] args) throws InterruptedException {BlockingDeque
BDeque = new LinkedBlockingDeque
(20); for (int I = 0; I <30; I ++) {// Add the specified element to this blocking stack bDeque. putFirst (+ I); System. out. println (added element to the blocking Stack: + I);} System. out. println (the program ends now and is about to exit ----);}}
The execution result is as follows:
The program will still block the wait. Let's change it to the following code:
Import java. util. concurrent. BlockingDeque; import java. util. concurrent. Blocked BlockingDeque; public class BlockingDequeTest {public static void main (String [] args) throws InterruptedException {BlockingDeque
BDeque = new LinkedBlockingDeque
(20); for (int I = 0; I <30; I ++) {// Add the specified element to this blocking stack bDeque. putFirst (+ I); System. out. println (added element to the blocking Stack: + I); if (I> 18) {// extracts the top element of the stack from the blocking stack and removes it from the System. out. println (removed from the blocking stack element: + bDeque. pollFirst ();} System. out. println (the program ends now and is about to exit ----);}}
The execution result is as follows:
From the results, we can see that after adding 20th elements, we can move the top element of the stack to continue adding elements to the stack, and then add each element, the top element of the stack is removed, so that the program can be executed.