Java multithreading-new features-blocking queue Arrayblockingqueue

Source: Internet
Author: User

The blocking queue is the content in the assigns feature of the Java5 line, and Java defines the interface java.util.concurrent.BlockingQueue that blocks the queue, and the concept of a blocking queue is a queue of a specified length, if the queue is full, The action to add a new element is blocked waiting until there is a vacancy. Similarly, when the queue is empty, the operation of the request queue element also blocks the wait until there are available elements.

With this feature, it is useful to open up a convenient channel for multi-threaded queued model implementations.

Java.util.concurrent.BlockingQueue inherits the Java.util.Queue interface and can refer to the API documentation.

Package Cn.thread;import Java.util.concurrent.arrayblockingqueue;import java.util.concurrent.blockingqueue;/** * Block Queue *  * @author Lin Yi-chin * @version 1.0 2013-7-25 pm 04:59:02 */public class Arrayblockingqueuetest {public    static Voi D main (string[] args) throws interruptedexception {        Blockingqueue queue = new Arrayblockingqueue;        for (int i = 0; i <; i++) {            //adds the specified element to this queue, if there is no free space, waits (if necessary).            queue.put (i);            System.out.println ("element added to the blocking queue:" + i);        }        System.out.println ("program runs to this end, will exit----");}    }
Added elements to the blocking queue: 0 added elements to the blocking queue: 1 Adds an element to the blocking queue: 2 Adds an element to the blocking queue: 3 Adds an element to the blocking queue: 4 Adds an element to the blocking queue: 5 Adds an element to the blocking queue: 6 Adds an element to the blocking queue : 7 Adds an element to the blocking queue: 8 Adds an element to the blocking queue: 9 Adds an element to the blocking queue: 10 adds an element to the blocking queue: 11 adds an element to the blocking queue: 12 adds an element to the blocking queue: 13 adds an element to the blocking queue: 14 Adds an element to the blocking queue : 15 adds an element to the blocking queue: 16 Adds an element to the blocking queue: 17 Adds an element to the blocking queue: 18 adds an element to the blocking queue: 19

As you can see, the output to element 19 is always waiting, because the queue is full and the program is blocked.

Java multithreading-new features-blocking queue Arrayblockingqueue

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.