If I store a concurrent hashmap in a httpsession then how can I use this hashmap in a threadsafe manner? by using the hashmap I mean adding and retrieving from the hashmap in a threadsafe manner.
what object should I lock on while getitng/putting in this hashmap?
is this code good:
private static final String SESSION_KEY_USER_IDENT = "CloudIdentityUserListMap";
private someSessionPopulateFunc()
{
final Object lock = sess.getHttpSession().getId().intern();
ConcurrentHashMap<String, List<User>> cloudIdentityUserListMap;
if (sess.getAttribute(SESSION_KEY_USER_IDENT) != null) {
synchronized (lock) {
cloudIdentityUserListMap = (ConcurrentHashMap<String, List<User>>) sess.getAttribute(SESSION_KEY_USER_IDENT);
}
} else {
cloudIdentityUserListMap = new ConcurrentHashMap<String, List<User>>();
synchronized (lock) {
sess.setAttribute (SESSION_KEY_USER_IDENT, cloudIdentityUserListMap);
}
}
cloudIdentityUserListMap.putIfAbsent(cloudIdentity,users);
}
Will I need to acquire lock on sess.getHttpSession().getId().intern() again while reading value from this concurrenthashmap?