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

相关文章:

  • 2022-12-23
  • 2021-11-17
  • 2022-12-23
  • 2021-06-04
  • 2021-12-05
  • 2022-12-23
  • 2021-10-13
猜你喜欢
  • 2021-11-28
  • 2022-01-05
  • 2021-06-01
  • 2021-08-20
  • 2022-01-03
  • 2021-12-18
相关资源
相似解决方案