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

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -