Your class will not be thread safe if you mark it as singleton in the context, since you initialize the fields with " new
" manually, which happen once when the bean is created, and you will have one instance in memory, such as your singleton, and accordingly your threads share an instance of CustomGxSessionIdCacheImpl, PcrfRecord, etc.
If you can get these instances to take control of the spring context, for example:
<bean id="customGxSessionIdCache" class="package.CustomGxSessionIdCacheImpl" scope="prototype">
and open them in PcrfSimulator, for example:
@Autowired private final CustomGxSessionIdCacheImpl gxSessionIdCache
after that, as soon as you access the code in gxSessionIdCache, spring creates a new instance for each access and for each thread, respectively. Any other methods in Singleton should have been synchronized
marked, as they are open to multithreaded interaction. spring singletones are regular single point.
I think itβs wrong to say, if you have no condition at all, then everything is thread-safe. If you think the level is low, the methods also contain states, that is, local variables, and if several threads access them, you can also get a headache.
source share