Queue<String> queue = new ArrayBlockingQueue<String>(3);    
        queue.add("abc");
        queue.add("eee");
        queue.add("fff"); 
        queue.add("efa");  
        System.out.println(queue);在你向队列add一个元素的时候,queue会先检查当前队列的长度,要是达到你之前定义的长度,则会抛异常,告诉你queue full

解决方案 »

  1.   


    上面,当加第四个元素"efa"时,在例行检查队列长度时,发现队列已满,抛异常
      

  2.   

    用LinkedBlockingQueue试试:
    Queue<String> list = new LinkedBlockingQueue<String>(2); 
      

  3.   

    必须用ArrayBlockingQueue吗?我的LinkedList不行对吗?
      

  4.   

    这个方法做到。但是我在书上并没有看到LinkedBlockingQueue与ArrayBlockingQueue,所以根本就不知道用这两个方法。
      

  5.   

     /**
         * Adds the specified element as the tail (last element) of this list.
         *
         * @param e the element to add
         * @return {@code true} (as specified by {@link Queue#offer})
         * @since 1.5
         */
        public boolean offer(E e) {
            return add(e);
        }这段是offer方法的源码,jdk1.5之后跟直接使用add是无什么区别的。不要尽信书,有时也不必去验证书上的每个理论,注重实际使用。如果你要指定队列的大小,满了之后要执行相应的逻辑,可以使用楼上的方法,cache到异常执行相应的操作就行。
      

  6.   

    自己定义一个常量设置队列的大小,要添加的时候,判断likedlist的size是否大于这个常量
      

  7.   

    if(list.size==3) throws Exception是不是就行了