Java EE does not provide you with any solution for competing resources over your own resources; but Java does.
In your case, using ConcurrentHashMap can solve most of your problems. A ConcurrentHashMap will protect you from cases where two threads are updating the Map exactly the same time (which in HashMap will most likely throw an exception). It offers you the same methods from the Map interface as HashMap , as well as some useful methods from the ConcurrentMap interface (for example, replace and putIfAbsent ). For most needs, this option is sufficient.
Having said that sometimes you might need to use the synchronized , even if you use ConcurrentHashMap . For example, consider the case where you want to put two elements in a Map , but it is extremely important for you that although the current stream returns two put s, no other stream will get or put from the map. In other words, ConcurrentHashMap only isolates access for each call individually; for cases where you need isolation to enable multiple calls, use synchronized .
EDIT after the comment by @Arjan: if you are using JavaEE 6.0, you can use @Singleton in combination with @Lock to achieve a similar effect.
Isaac source share