[Java concurrent programming] 21: New Features of concurrency-blocking queue and blocking stack (including code)

Source: Internet
Author: User

 

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.

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.