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 1809 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?

Updated on 25-Dec-2015
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By