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