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

Jason Lowe commented on HADOOP-10169:
-------------------------------------

As Jing pointed out, the function needs to be synchronized as written.  It's 
true that once initialized the code will no longer modify the map, but there's 
a race during the initialization itself.  If two or more threads call this 
function before it's initialized then they can both attempt to put() at the 
same time.  Alternatively, a thread could be late to the party and be calling 
get() just as the initializing thread is calling put() which is also a thread 
safety violation.

One possible way to avoid the synchronization bottleneck is to use a volatile 
boolean to indicate whether it's initialized so the vast majority of callers 
don't have to grab a lock.  Something like this pseudo-code:

{code}
volatile boolean isInitialized = false;

  if (!isInitialized) {
    synchronized (gcInfoCache) {
      if (!isInitialized) {
        .....
        gcInfoCache.put(...);
      }
    }
    isInitialized = true;
  }
  return gcInfoCache.get(...);
{code}

> remove the unnecessary  synchronized in JvmMetrics class
> --------------------------------------------------------
>
>                 Key: HADOOP-10169
>                 URL: https://issues.apache.org/jira/browse/HADOOP-10169
>             Project: Hadoop Common
>          Issue Type: Improvement
>          Components: metrics
>    Affects Versions: 3.0.0, 2.2.0
>            Reporter: Liang Xie
>            Assignee: Liang Xie
>            Priority: Minor
>         Attachments: HADOOP-10169.txt
>
>
> When i looked into a HBase JvmMetric impl, just found this synchronized seems 
> not essential.



--
This message was sent by Atlassian JIRA
(v6.1.4#6159)

Reply via email to