self-confidence,the source of all the power

导航

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");

posted on 2012-08-20 19:27  漩涡鸣人  阅读(394)  评论(0编辑  收藏  举报