I'm trying to create a fine grain locking mechanism for the following scenario:
I have a data store with many serialised Cache
objects inside it. Each Cache
belongs to a certain person, group or company and each Cache
can be modified in one of four ways: they can be created, deleted, removed from or inserted into. While a Cache
is being modified I want to block access to it. Each Cache
is identified using a CacheLocation
object which stores the directory and file name as well as the full path for convenience.
Currently I am using an array list inside a class called RequestQueue
which holds current CacheLocation
objects being processed. Then when another thread comes in it checks the queue to see if the CacheLocation
it is requesting is already being used. If this is the case a while loop is used to keep checking the CacheLocation
periodically until the request that put it there removes it.
I was thinking it might be an idea to have a HashMap of CacheLocation
keys against BlockingQueue
values. This would result in a large set of BlockingQueue
objects but I could manage the queue fairly well.
Is there a better way to do this sort of fine grain locking?
If I understand your description correctly, one way that would keep your design fairly simple would be to:
ConcurrentHashMap<CacheLocation, Cache>
to store the Caches (I assume CacheLocation
s are immutable, or at least never mutated)CacheLocation
object