Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java: updating a hashmap automatically

so right now i have a hashmap which stores Calendar objects (just need a way to store dates). each value stores two Calendar objects, one representing the start of an event, and the other the end of it. The user enters these through a gui, and they can span weeks and months.

What i need to do is be able to remove these entries when they expire, meaning their second Calendar object has passed according to the real world date. I want this to be done automatically as long the instance is running.

My idea was to have a timer thread running and once a week it could go through all the entries in the hashmap and remove the ones that have expired. I know its not all that practical, its for a school project. But I was just looking for ideas or design pattern that could help implement this in an efficient manner.

Thanks and let me know if you need any more information.

like image 924
user1769946 Avatar asked Jul 17 '26 17:07

user1769946


1 Answers

Having a background thread that performs maintenance tasks is a good idea and is very common practice. Be warned though that a Hashmap is not thread safe so you will need to synchronize its access, or replace with a thread safe class such as ConcurrentHashMap.

like image 155
Darren Avatar answered Jul 20 '26 05:07

Darren