I have a HashMap which is static and three threads which try to access HashMap simultaneously from their corresponding class`s.
each thread task is get list value of a specified key, process some operations on the list(modify the list). and put the processed list in HashMap.
I want to make other threads trying to access the HashMap wait until current thread finishes the processing and modifying the HashMap.
in some situation, the flow is like this,
thread A is retrieved HashMap, while Thread A is processing on the list of HashMap, other Thread B retrieves the HashMap and starts its processing.
Actual behaviour has to be like:
Thread A -> retrieves HashMap -> process -> put value in HashMap.
Thread B -> retrieves HashMap -> process -> put value in HashMap.
Thread C -> retrieves HashMap -> process -> put value in HashMap.
logic :
- apply lock on HashMap
- retrieve.
- process.
- put into HashMap.
- release lock.
help me in converting the logic to code, or any suggestions are accepted with smile.
ConcurrentHashMap
?ConcurrentHashMap
takes care of locking the hashMap only until that particular statement runs. but in my class I want the lock to be held until it puts the processed data into hashMap.