Is it necessary to use ConcurrentHashMap to implement the policy pattern with SpringIOC

is currently a virtual base class of Service
there are three implementations, the class attributes are all
of Autowire and then injected into Map to call
this Map, is it necessary to use ConcurrentHashMap?
generally speaking, Service is a singleton, so there should be no problems caused by multithreading
, but when I just read a post, I used ConcurrentHashMap and didn"t say why
, so I wanted to ask

.
Jul.06,2022

in general Service is thread safe, not because it is singleton, but because Service is basically stateless in anaemic models.
assume that you injected Service into Map in the following way:

@Autowired
private Map<String,TypeInterface> services;

this Map needs ConcurrentHashMap depends on how you use this services , assuming that you do not update the contents of services after initialization, but just get service , then there is no need to use ConcurrentHashMap . On the other hand, if you update this Map in multiple threads later, you need to use ConcurrentHashMap .


many bean in the Spring project are designed to be thread-safe after configuration , that is, in the initialization phase of Spring, because Spring itself is single-threaded initialization and will not encounter problems, after initialization is completed, it is no longer modified, and the properties are read-only, so there is no race problem caused by multi-thread modification. You have to make sure that you don't need ConcurrentHashMap when using it, and vice versa.


Thread safety is relative, Bean is singleton, but you can't control the thread condition when calling methods in singletons, just like you want to modify the same properties in an instance at the same time.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bc2042-305de.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bc2042-305de.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?