java 多线程 阻塞队列
code:
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Queue {
public static void main(String[] args) {
BlockingQueue bqueue = new ArrayBlockingQueue(20);
for (int i = 0; i < 30; i++) {
try {
bqueue.put(i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("add items into queue:" + i);
if (bqueue.size()==20) {
for (int j = 0; j<5;j++) {
System.out.println("remove items from queue:" + bqueue.remove());
}
}
}
System.out.println("program is over");
}
}
说明:队列满,线程将被阻塞,所以只有加入了出队操作程序才会被执行到最后一行----System.out.println("program is over");