Do you want every item in the hash map to be cleared at midnight?
If I think so, I would create a new hash file and replace it with the original. Since the substitution is done in one step and nothing else is trying to make the same change, I believe that it should be thread safe.
Anything in the middle access will be allowed to complete its access. This suggests that nothing else refers to this hash map.
If the hasmap links are not combined in one place, then I think hashmap.clear() is the best bet if the hash map is synchronized.
If the hashmap is not synchronized, you can change it from only one thread, right? Ask this thread to call hashmap.clear() .
I think it covers all cases.
source share