Java thread (22): new feature-blocking queue

Source: Internet
Author: User

Java thread: new feature-blocking queue is the content of the new feature of Java 5 threads. java defines the interface for blocking queues Java. util. concurrent. blockingQueue: The concept of blocking queue is that a queue with a specified length will be blocked and wait until there is a blank space. Similarly, when the queue is empty, operations on the Request queue element will also block waiting until there are available elements. With this function, it opens a convenient channel for implementing the model of multi-thread queuing waiting, which is very useful. Java. util. concurrent. BlockingQueue inherits the java. util. Queue interface. For more information, see the API documentation. The following is an example of a simple application: import java. util. concurrent. blockingQueue; import java. util. concurrent. arrayBlockingQueue;/*** Java thread: new feature-blocking queue ** @ author leizhimin 14:59:15 */public class Test {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, if there is no available space, it will remain waiting (if necessary ). Bqueue. put (I); System. out. println ("added element to the blocked queue:" + I);} System. out. println ("the program ends now and is about to exit ----") ;}} output result: element: 0 is added to the blocking queue: 1. An element is added to the blocking queue. 2. An element is added to the blocking queue. 3. An element is added to the blocking queue. 4. An element is added to the blocking queue: 5. added element to the blocking queue: 6. added element to the blocking queue: 7. added element to the blocking queue: 8. added element to the blocking queue: 9. An element is added to the blocking queue. element: 10 is added to the blocking queue. element: 11 is added to the blocking queue. element: 12 is added to the blocking queue: 13. element added to the blocking queue: 14. element added to the blocking queue: 15. element added to the blocking queue: 16. element added to the blocking queue: 17. added element to the blocking queue: 18 to the blocking queue: 19 As you can see, when the output is to element 19, it is always in the waiting state, because the queue is full and the program is blocked. Multithreading is not used for demonstration. This is not necessary. In addition, there are more implementation classes for blocking queues to meet various complex requirements: ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue, PriorityBlockingQueue, and SynchronousQueue. The specific API differences are also small.

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.