java - synchrozie get method from map? -



in order synchrozie map method use

myobj = mymap.get("aaa");   if (myobj==null)     synchronize (someobject){         myobj = mymap.get("aaa");           if (myobj==null){             myobj = createnew();             mymap.put(myobj);      }  } return myobj; 

is besdt way. asking null condition twice?

synchronizing get isn't useful. get not change state of map. have synchronize put (with same object monitor). , can use collections.synchronizedmap(..) or concurrenthashmap that

update: get & put can use concurrenthashmap.putifabsent(..)


Comments