What is best way to implement thread-safe LRUCache in java? Please review this one. Is there any better approach which can be taken here?

package cache;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;

public class  LRUCache<K,V> {

private  ConcurrentHashMap<K,V> concurrentHashMap = new ConcurrentHashMap<K, V>();

int maxSize=0;

public LRUCache(final int MAX_SIZE){
this.maxSize=MAX_SIZE;
}

public V getElement(K key){

try {
V v=null;
if(concurrentHashMap.contains(key)){
v= concurrentHashMap.get(key);
}

return v;
}finally{
}
}

public V removeElement(K key){
writeLock.lock();
try {
V v=null;
if(concurrentHashMap.contains(key)){
v=concurrentHashMap.remove(key);
}

return v;
} finally {
writeLock.unlock();
}
}

writeLock.lock();
try {
if(concurrentHashMap.contains(key)){
}
concurrentHashMap.remove(queueKey);
}
concurrentHashMap.put(key, value);

return value;
} finally{
writeLock.unlock();
}
}
}


This implementation is not thread safe. For example, if two threads invoked getElement(key1) simultaneously, they may runs to concurrentLinkedQueue.add(key) at same times(they both performed delete and get, but delete will fail the second time or just do nothing), then two same keys will be added.

Maybe capacity is more suitable than maxSize.

LinkedHashMap is often used as LRU cache.

• Why does the number 1 happen? Doesn't the key get removed and added? Isn't concurrent containers thread safe. – 422_unprocessable_entity Mar 11 at 9:02
• @422_unprocessable_entity the three operations inside if block is not atomic. If two threads running to concurrentLinkedQueue.add(key); at same times(they both performed delete and get, but delete will fail the second time or just do nothing), two same keys will be added. – lauthu Mar 11 at 9:33
• Yes you are correct. Well done on catching it. :) Can you insert the explanation in the answer as well. – 422_unprocessable_entity Mar 11 at 9:50

public class  LRUCache<K,V>


You have two spaces between class and LRUCache.

v=null

In professional Java programming, it is customary to have a single space before and after each binary operator.

public V getElement(K key){

try { ... } ...


Looks strange to me. Why not have instead the following:

public V getElement(K key){

try { ... } ...


int maxSize=0;


First of all, I suggest you make it private. Also, there is no need to initialize member ints with zero, that is done by Java by default. Other fundamental types are initialized to zero too.

public LRUCache(final int MAX_SIZE)


It is idiomatic to name only constants with UPPER CASE. Constructor (and method) parameters are advised to be in camelCase.

private  ConcurrentHashMap<K,V> concurrentHashMap = new ConcurrentHashMap<K, V>();


You could omit type parameters in new ConcurrentHashMap<K, V> thanks to diamond inference. Also, you could declare all the members instead of maxSize to final:

private final ConcurrentHashMap<K,V> concurrentHashMap = new ConcurrentHashMap<>();


ConcurrentHashMap<K,V>

It is also customary to have a single space after each comma character: <K,_V>.
concurrentLinkedQueue and concurrentHashMap. These are not the best possible names for the two data structures. Instead of naming them according to their type I instead suggest to name them according to what they do.