I am working on my first mutlithreaded program and fixated on several aspects of synchronization. I went over to the multi-threaded tutorial on the oracle / sun homepage, as well as a number of questions here on SO, so I think I have an idea of what synchronization is. However, as I mentioned, there are several aspects, I’m not quite sure how to understand this. I formulated them below as a clear question:
Question 1: I have a singleton class that contains methods for checking valid identifiers. It turns out that this class needs to be stored in collections in order to track associations between two different types of identifiers. (If the word identifier sounds more complicated, it's just a string). I decided to implement two instances of MultiValueMap to implement this many-to-many relationship. I'm not sure that these collections should be thread safe, since the collection will only be updated when instantiating a singleton class, but nonetheless I noticed that the documentation says:
Please note that MultiValueMap is not synchronized and is not thread safe. If you want to use this card from several streams at the same time, you should use appropriate synchronization. This class may throw exceptions when accessed by parallel threads without synchronization.
Can anyone clarify this “appropriate synchronization”? What exactly does this mean? Can't I use MultiValueMap.decorate() for a synchronized HashMap , or am I misunderstanding something?
Question 2: I have another class that extends HashMap to store my experimental values, which are analyzed when the software starts. This class is intended to provide suitable methods for my analysis, such as permutation() , randomization() , filtering(criteria) , etc. Since I want to protect my data as much as possible, the class is created and updated once, and all of the above methods return new collections. Again, I am not sure that this class should be thread safe, since it should not be updated from multiple threads, but the methods will certainly be called from multiple threads, and to be "safe", I added synchronized for all my methods. Can you anticipate any problems with this? What potential problems should I know?
Thanks,
source share