[英]Is there a bounded non-blocking Collection in Java?
The only one I can find is the BoundedFIFOBuffer, which is deprecated. 我唯一能找到的是BoundedFIFOBuffer,它已被弃用。 Any others?
还有其他人?
BoundedFIFOBuffer
in Apache Commons Collections (which I assume is what you're referring to) is not deprecated, it has just moved packages. Apache Commons Collections中的
BoundedFIFOBuffer
(我假设你所指的是)并没有被弃用,它只是移动了包。 The original one in org.apache.commons.collections
is deprecated, and has instead been moved to org.apache.commons.collections.buffer
不推荐使用
org.apache.commons.collections
的原始版本,而是将其移至org.apache.commons.collections.buffer
Why not just use a LinkedBlockingQueue
and use the non-blocking methods offer
(or add
) and poll
to access it? 为什么不直接使用
LinkedBlockingQueue
并使用非阻塞方法offer
(或add
)和poll
来访问它? You can create it with a fixed capacity (ie to make it bounded). 您可以使用固定容量创建它(即使其有界)。
There are some bounded collections in Apache commons-collections , including a BoundedFifoBuffer . Apache commons-collections中有一些有界集合,包括BoundedFifoBuffer 。
In the same library, is also BoundedBuffer and CircularFifoBuffer 在同一个库中,也是BoundedBuffer和CircularFifoBuffer
I've been using Google Collections recently. 我最近一直在使用Google Collections 。 I think you could have a Java Generics solution pretty easily with it.
我认为你可以很容易地使用Java Generics解决方案。 There is a class called ForwardingList which I think you could implement this idea pretty easily.
有一个名为ForwardingList的类,我认为你可以很容易地实现这个想法。 Obviously not as easy as just using BoundedFifoBuffer (non-generic) or ArrayBlockingQueue.
显然不像使用BoundedFifoBuffer(非泛型)或ArrayBlockingQueue那么容易。
final ArrayList<MyObject> realList = Lists.newArrayList();
final List<MyObject> list = new ForwardingList<MyObject>() {
protected List<MyObject> delegate() {
return realList;
}
public boolean add(MyObject obj) {
if(delegate().size() > 100) return false;
return delegate().add(obj);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.