Is there a bounded non-blocking Collection in Java?

前端 未结 4 1432
情话喂你
情话喂你 2020-12-10 13:56

The only one I can find is the BoundedFIFOBuffer, which is deprecated. Any others?

4条回答
  •  忘掉有多难
    2020-12-10 14:42

    I've been using Google Collections recently. I think you could have a Java Generics solution pretty easily with it. There is a class called ForwardingList which I think you could implement this idea pretty easily. Obviously not as easy as just using BoundedFifoBuffer (non-generic) or ArrayBlockingQueue.

    final ArrayList realList = Lists.newArrayList();
    final List list = new ForwardingList() {
        protected List delegate() {
            return realList;
        }
    
        public boolean add(MyObject obj) {
            if(delegate().size() > 100) return false;
            return delegate().add(obj);
        }
    };
    

提交回复
热议问题