java.util.Collections.synchronizedMap(map) returns a synchronized (thread-safe) map backed by the specified map.
You can anonymously extend
LinkedHashMap
to change the behavior of removeEldestEntry(...)
, then wrap the instance of the anonymous class in a synchronized map. You didn't mention what type parameters you require, so I'm using <String, Integer>
in this example.Map<String, Integer> map = Collections.synchronizedMap(new LinkedHashMap<String, Integer>() {
private static final long serialVersionUID = 12345L; // use something random or just suppress the warning
@Override
protected boolean removeEldestEntry(Entry<String, Integer> eldest) {
return size() > MAX_SIZE; // how many entries you want to keep
}
});
Original Link:http://stackoverflow.com/questions/28653889/java-thread-safe-linkedhashmap-implementation
No comments:
Post a Comment