Java Threads: New features-blocking stacks

Source: Internet
Author: User

For blocking stacks, this is similar to blocking queues. The difference is that the stack is "after the first out" structure, each operation is the top of the stack, and the queue is "FIFO" structure, each operation is the queue head.

Specifically, the blocking stack is a new feature of the JAVA6. 、

Java for the blocking stack defines the interface: Java.util.concurrent.BlockingDeque, the implementation of the class is also more specific, you can view the JAVAAPI document.

Let's look at a simple example:

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
/**
* Java线程:新特征-阻塞栈
*
* @author leizhimin
*/
public class Test {
         public static void main(String[] args) throws InterruptedException {
                 BlockingDeque bDeque = new LinkedBlockingDeque(20);
                 for (int i = 0; i < 30; i++) {
                         //将指定元素添加到此阻塞栈中,如果没有可用空间,将一直等待(如果有必要)。
                         bDeque.putFirst(i);
                         System.out.println("向阻塞栈中添加了元素:" + i);
                 }
                 System.out.println("程序到此运行结束,即将退出----");
         }
}

Output results:

向阻塞栈中添加了元素:0
向阻塞栈中添加了元素:1
向阻塞栈中添加了元素:2
向阻塞栈中添加了元素:3
向阻塞栈中添加了元素:4
向阻塞栈中添加了元素:5
向阻塞栈中添加了元素:6
向阻塞栈中添加了元素:7
向阻塞栈中添加了元素:8
向阻塞栈中添加了元素:9
向阻塞栈中添加了元素:10
向阻塞栈中添加了元素:11
向阻塞栈中添加了元素:12
向阻塞栈中添加了元素:13
向阻塞栈中添加了元素:14
向阻塞栈中添加了元素:15
向阻塞栈中添加了元素:16
向阻塞栈中添加了元素:17
向阻塞栈中添加了元素:18
向阻塞栈中添加了元素:19

From the above results can be seen, the program does not end, the second is blocked, because the stack is full, the subsequent additions to the operation of the elements are blocked.

Source: http://lavasoft.blog.51cto.com/62575/222530

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.