title: Thread-safe HashMap access
body: <p>I have a class which wraps up a map. The map is read/written by an Add() and isUpwardTrade() methods as indicated below.</p>
  
  <p>Do you see any thread safety issues by synchronizing the whole methods?
  How would you change the following implementation (i.e. would you use a concurrentHashMap or something else? ) to improve the  performance in a multi-threaded context?</p>
  
  <pre><code>private Map&lt;String, List&lt;Double&gt;&gt; priceTable = new HashMap&lt;String, List&lt;Double&gt;&gt;();
  private AutoTrader autoTrader;
  
  public PriceTable(AutoTrader autoTrader) {
      this.autoTrader = autoTrader;
  }
  
  public synchronized void add(Price price) {     
      if (!priceTable.containsKey(price.getProductName())){
          List&lt;Double&gt; prices = new ArrayList&lt;Double&gt;();
          Double pValue = price.getPrice();
          prices.add(pValue);
          priceTable.put(price.getProductName(), prices);
      }else{
          Double pValue = price.getPrice();
          priceTable.get(price.getProductName()).add(pValue);
      }
  
      if (isUpwardTrend(price, priceTable)) {
          notifyAutoTrader(price);
      }
  }
  
  private void notifyAutoTrader(Price price) {
       autoTrader.onUpwardTrendEvent(price);
  }
  
  private synchronized boolean  isUpwardTrend(Price price, Map&lt;String, List&lt;Double&gt;&gt;   pricesTable) {
      List&lt;Double&gt; prices = priceTable.get(price.getProductName());
      if ( prices.size() &gt;= 4){
          if ( calcAvg(prices)  &gt; prices.get(prices.size() - 4) ) 
             return true;
      }
      return false;
  }
  </code></pre>