[ 
https://issues.apache.org/jira/browse/ZOOKEEPER-1978?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14070692#comment-14070692
 ] 

Hongchao Deng commented on ZOOKEEPER-1978:
------------------------------------------

[~rakeshr][~phunt]
Here is my conclusion,

Basically, ConcurrentHashMap is thread safe without synchronizing the whole 
map. Reads can happen very fast while write is done with a lock. I prefer 
keeping concurrent HashMap to adding another synchronized method.

So far, this warning is a fake one because it's under synchronized 
addSession(). Another option is to disable the warning. But I doubt if 
disabling warning is a good practice.

h4. The ideal solution
Well, the ideal solution to the problem is:
The concurrent map should support
{code}
putIfAbsent( key, () -> value )
{code}
So if the key doesn't exist, the putIfAbsent method would call the expensive 
computation and put the value. This is unrealistic in current JDK.

My conclusion is that creating a simple object like SessionImpl is fine. It's a 
cheaper overhead among all the tradeoff here.
I realized that Java 8 has the ideal solution:
http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#computeIfAbsent-K-java.util.function.Function-
So keeping a similar workflow would be easy to refactor once JDK is upgraded.

> Fix Multithreaded correctness Warnings
> --------------------------------------
>
>                 Key: ZOOKEEPER-1978
>                 URL: https://issues.apache.org/jira/browse/ZOOKEEPER-1978
>             Project: ZooKeeper
>          Issue Type: Sub-task
>            Reporter: Hongchao Deng
>            Assignee: Hongchao Deng
>            Priority: Minor
>             Fix For: 3.5.0
>
>         Attachments: ZOOKEEPER-1978.patch
>
>
> findbugs is complaining
> {code}
>  if (sessionsById.get(id) == null) {
>              SessionImpl s = new SessionImpl(id, sessionTimeout);
>              sessionsById.put(id, s);
> }
> {code}
> is not atomic for the gap between get() and put().
> I suggest using putIfAbsent() instead.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to