Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a big hashmap in AS

Tags:

java

hashmap

I have a hashmap which stores around 1 G of data is terms of key value pairs. This hashmap changes every 15 days. It will be loaded into memory and used from there.

When a new hashmap has to be loaded into the memory, there would be several transactions already accessing the hashmap in memory. How can I replace the old hashmap with the new one without effecting the current transactions accessing the old hashmap. If there a way to hot swap the hashmap in memory?

like image 344
Abhi Avatar asked Oct 03 '22 09:10

Abhi


1 Answers

Use an AtomicReference<Map<Foo, Bar>> rather than exposing a direct (hard) reference to the map. Consumers of the map will use #get(), and when you're ready to swap out the map, your "internal" code will use #set() or #getAndSet().

like image 188
Matt Ball Avatar answered Oct 07 '22 17:10

Matt Ball