Java: How to atomically replace all values in a Map?

后端 未结 6 1282
忘了有多久
忘了有多久 2020-12-11 07:04

I have a stateful bean in an multi-threaded enviroment, which keeps its state in a map. Now I need a way to replace all values of that map in one atomic action.

         


        
6条回答
  •  时光取名叫无心
    2020-12-11 07:29

    As client code maintains a reference to the bean not the map, replacing the value (i.e. the whole map) would seem to be the simplest solution.

    Unless there's any significant performance concerns (although using locking is likely to perform worse and less predictably unless the map is huge) I'd try that before anything requiring more advanced knowledge.

    It's how a functional programmer would do it.

提交回复
热议问题