I have developed an LRU cache in java now please advise I have to customize it as per the multi threaded revilement so please advise what changes I need to to in my below program to make it safe for multi threaded environment , below is my code ..
import java.util.LinkedHashMap;
public class LRUCache extends LinkedHashMap<Integer, String> {
private static final long serialVersionUID = 1342L;
private int cacheSize;
//In overridden method, we are saying that, remove entry only when we have reached cacheSize.
//initialCapacity,loadFactor,accessOrder the ordering mode - true for
// access-order, false for insertion-order
public LRUCache(int size) {
super(size, 0.75f, true);
this.cacheSize = size;
}
@Override
// removeEldestEntry() should be overridden by the user, otherwise it will not
//remove the oldest object from the Map.
protected boolean removeEldestEntry(java.util.Map.Entry<Integer,String> eldest) {
return size() > cacheSize;
}
public static void main(String arg[]){
LRUCache lruCache = new LRUCache(2);
lruCache.put(1, "Object1");
lruCache.put(2, "Object2");
lruCache.put(3, "Object3");
System.out.println(lruCache);
}
}