msbaek71 wrote:
Hi
Q1. Is there any solution tp lock an entry in the map instead of locking entire map ?
Absolutely. There is a best practice around this. You should use a read lock around your map.get, and then a write lock on the method for the Count object.
For example:
Code:
public class Count
{
int count = 0;
public synchronized increment() { count++; }
public synchronized decrement() { count--; }
}
public MyClass
{
...
public void incrementCount(String key)
{
Count count;
synchronized (map)
count = (Count) map.get(key);
}
count.increment();
}
}
In the above code, you should put a read lock on the MyClass.incrementCount method, and write locks on the Count.increment and Count.decrement methods.
msbaek71 wrote:
Q2. Can I use concurrent lock level if there're no solution for Q1 ?
The count object in the map has only increase and decrease method. I don't mind the sequece of method calls.
In general you shouldn't use the concurrent lock level.