Java concurrency Programming (18) blocking queues and blocking stacks

Source: Internet
Author: User

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:

    ImportJava.util.concurrent.BlockingQueue; ImportJava.util.concurrent.ArrayBlockingQueue;  Public classblockingqueuetest{ Public Static voidMain (string[] args)throwsinterruptedexception {blockingqueue<String> Bqueue =NewArrayblockingqueue<string> (20);  for(inti = 0; I < 30; i++) {                               //adds the specified element to this queueBqueue.put ("add element" +i); System.out.println ("added elements to the blocking queue:" +i); } System.out.println ("The program ends at this run and is about to 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:

    ImportJava.util.concurrent.BlockingQueue; ImportJava.util.concurrent.ArrayBlockingQueue;  Public classblockingqueuetest{ Public Static voidMain (string[] args)throwsinterruptedexception {blockingqueue<String> Bqueue =NewArrayblockingqueue<string> (20);  for(inti = 0; I < 30; i++) {                               //adds the specified element to this queueBqueue.put ("" +i); System.out.println ("added elements to the blocking queue:" +i); if(I > 18){                                  //get the team head element from the queue and move it out of the queueSystem.out.println ("Remove elements from the blocking queue:" +Bqueue.take ()); }} System.out.println ("The program ends at this run and is about to 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:

    ImportJava.util.concurrent.BlockingDeque; ImportJava.util.concurrent.LinkedBlockingDeque;  Public classBlockingdequetest { Public Static voidMain (string[] args)throwsinterruptedexception {blockingdeque<String> Bdeque =NewLinkedblockingdeque<string> (20);  for(inti = 0; I < 30; i++) {                       //adds the specified element to this blocking stackBdeque.putfirst ("" +i); System.out.println ("added elements to the blocking stack:" +i); } System.out.println ("The program ends at this run and is about to exit----"); }       }  

The results of the implementation are as follows:


The program will still block the wait, let's change to the following code:

    ImportJava.util.concurrent.BlockingDeque; ImportJava.util.concurrent.LinkedBlockingDeque;  Public classBlockingdequetest { Public Static voidMain (string[] args)throwsinterruptedexception {blockingdeque<String> Bdeque =NewLinkedblockingdeque<string> (20);  for(inti = 0; I < 30; i++) {                       //adds the specified element to this blocking stackBdeque.putfirst ("" +i); System.out.println ("added elements to the blocking stack:" +i); if(I > 18){                          //Remove the top element of the stack from the blocking stack and move it outSystem.out.println ("removed elements from the blocking stack:" +Bdeque.pollfirst ()); }} System.out.println ("The program ends at this run and is about to 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.

Java concurrency Programming (18) blocking queues and blocking stacks

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.