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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -