java - Examples of use thread safe singleton in multiple threads -
there thread safe singleton, when use singleton in multiple threads? when may helpful?
i use stateless singletons implementations of strategy. being stateless, thread safe , there no problem testing them.
where problems stateful singletons. leads more interactions, in multi-thread context, unit testing stateful singleton in 1 thread pain (i.e. have reset state between tests). reason, many see singletons anti-pattern avoided.
a better approach stateful singletons use dependency injection. way code building components create 1 object, , passed components need it.
Comments
Post a Comment