Turn: Java concurrency Programming 21: Concurrency new features-blocking queues and blocking stacks (with code)

Source: Internet
Author: User

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
  1. Import Java.util.concurrent.BlockingQueue;
  2. Import Java.util.concurrent.ArrayBlockingQueue;
  3. Public class blockingqueuetest{
  4. public static void Main (string[] args) throws interruptedexception {
  5. blockingqueue<string> bqueue = new arrayblockingqueue<string> (20);
  6. For (int i = 0; i < ; i++) {
  7. //Add the specified element to this queue
  8. Bqueue.put ("add element" + i);
  9. System.out.println ("element added to the blocking queue:" + i);
  10. }
  11. SYSTEM.OUT.PRINTLN ("program to this run end, will exit----");
  12. }
  13. }
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
  1. Import Java.util.concurrent.BlockingQueue;
  2. Import Java.util.concurrent.ArrayBlockingQueue;
  3. Public class blockingqueuetest{
  4. public static void Main (string[] args) throws interruptedexception {
  5. blockingqueue<string> bqueue = new arrayblockingqueue<string> (20);
  6. For (int i = 0; i < ; i++) {
  7. //Add the specified element to this queue
  8. Bqueue.put ("" + i);
  9. System.out.println ("element added to the blocking queue:" + i);
  10. if (i > ) {
  11. //Get the team head element from the queue and move it out of the queue
  12. System.out.println ("remove elements from the blocking queue:" + bqueue.take ());
  13. }
  14. }
  15. SYSTEM.OUT.PRINTLN ("program to this run end, will exit----");
  16. }
  17. }
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
  1. Import Java.util.concurrent.BlockingDeque;
  2. Import Java.util.concurrent.LinkedBlockingDeque;
  3. Public class Blockingdequetest {
  4. public static void Main (string[] args) throws interruptedexception {
  5. blockingdeque<string> bdeque = new linkedblockingdeque<string> (20);
  6. For (int i = 0; i < ; i++) {
  7. //Add the specified element to this blocking stack
  8. Bdeque.putfirst ("" + i);
  9. System.out.println ("added elements to the blocking stack:" + i);
  10. }
  11. SYSTEM.OUT.PRINTLN ("program to this run end, will exit----");
  12. }
  13. }
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
  1. Import Java.util.concurrent.BlockingDeque;
  2. Import Java.util.concurrent.LinkedBlockingDeque;
  3. Public class Blockingdequetest {
  4. public static void Main (string[] args) throws interruptedexception {
  5. blockingdeque<string> bdeque = new linkedblockingdeque<string> (20);
  6. For (int i = 0; i < ; i++) {
  7. //Add the specified element to this blocking stack
  8. Bdeque.putfirst ("" + i);
  9. System.out.println ("added elements to the blocking stack:" + i);
  10. if (i > ) {
  11. //Remove the top element of the stack from the blocking stack and move it out
  12. System.out.println ("removed elements from the blocking stack:" + bdeque.pollfirst ());
  13. }
  14. }
  15. SYSTEM.OUT.PRINTLN ("program to this run end, will exit----");
  16. }
  17. }
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)

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.