Users Pricing

forum

home / developersection / forums / wrap concurrentskiplistset to keep a fixed capacity of the latest values in a thread-safe way?

wrap ConcurrentSkipListSet to keep a fixed capacity of the latest values in a thread-safe way?

Anonymous User 2189 25 Dec 2015
I want to wrap ConcurrentSkipListSet to keep a fixed capacity of the latest (according to Comparator) values:
private int capacity = 100;
// using Integer just for an illustration
private ConcurrentSkipListSet<Integer> intSet = new ConcurrentSkipListSet<>();
Therefore, I implemented put() like this:

// This method should be atomic.
public void put(int value) {
    intSet.add(value);
    if (intSet.size() > capacity)
        intSet.pollFirst();
}
However, this put() is not thread-safe.

Note: No other mutation methods. Of course, I need "read-only" methods like getLast() or getBefore(Integer value).

How to wrap ConcurrentSkipListSet to keep a fixed capacity of the latest values in a thread-safe way?

I am a content writter !


1 Answers

Anonymous User 25 Dec 2015 Accepted Answer