Is there a parallel and self-limited and ordered hash map in java

I am using ConcurrentHashMapgoogle guava (via MapMaker), but this implementation is not streamlined. There is guava in google ConcurrentSkipListMap, but this structure cannot expire.

Is there any structure that can do both?

+3
source share
7 answers

I would use ConcurrentSkipListMapand repeat the card periodically to clear expired items. Since the skipist is unlimited, this opens up the possibility of memory when the eviction thread cannot catch up, but in practice it seems very, very unlikely if you are not doing something extreme. I did something similar too, when I did not do this want to use a background thread:

static AtomicInteger cnt = new AtomicInteger();

Val put(K key, V val){
    //Do the usual stuff
    if(cnt.getAndIncrement() % 100 == 0){
        //iterate map and evict stuff
    }
}

if(cnt.getAndIncrement() % 100 == 0), , " ", , , , b.

, , ... , :

class Entity implaments Comparable<Entity>{
    static AtomicInteger SEQ = new AtomicInteger();
    int id = SEQ.getAndIncrement();
    long timeStamp = System.currentTimeMillis();

    int compareTo(Entity other){
        // compare by timestamp
        // If timestamps are equal, don't just return 0 yet!!
        // Compare by id and return 0 ONLY IF the id equals.
        // Otherwise entities disappear from your ordered map and 
        // it really annoying to debug.
}
+2

java.util.LinkedHashMap removeEldestEntry(), , .

+1

, . , , .

0

( , ), , , - 60 , . , .

0

? . UUID . UUID .

0

, . ForwardingMap

0

. Guava, ExpiringMap. , , .

0

All Articles