Results 1 to 2 of 2
- 04-30-2011, 11:20 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
Synchronization utility to manage locks
Hey folks,
Say I have the following:
I am trying to handle concurrent calls to this method based on the id, meaning, concurrent calls to this method for the same ID should block, but concurrent calls to this method for different IDs should not block.Java Code:public void doSomething(Integer myObjID, String val) throws CustomException { MyObject myObj = getTheObject(myObjID); // get object from the database if(null == myObj.getVal()){ myObj.setVal(val); saveTheObject(myObj); // save to database } else{ throw new CustomException("The object with the given id already has a val."); } }
I am trying to find something like:
What I am thinking is that the Utility class creates locks for invocations of the method. Since the program is running indefinitely, locks need to be created and destroyed during the program execution.Java Code:public void doSomething(int myObjID, String val) throws CustomException { MyObjLock idBasedLock = Utility.getOrCreateLock(myObjID); synchronized(idBasedLock){ MyObject myObj = getTheObject(myObjID); // get object from the database if(null == myObj.getVal()){ myObj.setVal(val); saveTheObject(myObj); // save to database } else{ throw new CustomException("The object with the given id already has a val."); } } }
Since concurrent calls for the same ID need the same lock, the Utility class either:
- creates the lock, stores it in memory, then returns it
- returns the lock if it already exists
Since locks are created at runtime, the Utility class needs to have a way to get rid of the locks that are not being used. For example, if two concurrent calls for the same ID come in, the Utility class does 1 and 2 above. When the two threads finish running, we don't know how long it will be until another call comes in for the same ID, so the lock must be disposed.
I don't have a lot of experience with synchronization, but I was wondering if this is possible? I think it might be safer to have the database handle the concurrency issues around the field value, but the initial problem lead me to think about how to handle synchronization based on the value of a field on the fly.
I was thinking about having a ConcurrentHashMap keyed on an Integer:
With this kind of implementation, I am thinking the the actual lock object needs some kind of counter that indicates how many threads are using it.Java Code:public final class Utility { private ConcurrentHashMap<Integer, Object> keyMap = new ConcurrentHashMap<Integer, Object>(); private Utility(){ // empty } public static MyObjLock getOrCreateLock(Integer id){ keyMap.putIfAbsent(id, new MyObjLock()); return keyMap.get(id); } // some deamon thread to remove locks that are not being used from keyMap }
What I also thought of doing was using a WeakHashMap, but I have never worked with them before and it would have to deal with synchronization.
Any thoughts? Does this make any sense or am I off my rocker?Last edited by JSL5101; 05-01-2011 at 07:14 PM.
- 05-07-2011, 05:43 PM #2
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Problems using wait() and notifyAll() with locks
By cuffJ in forum Threads and SynchronizationReplies: 6Last Post: 08-18-2010, 03:21 AM -
XML compare utility
By JMayura in forum Advanced JavaReplies: 0Last Post: 06-08-2010, 01:05 PM -
InterruptedException and Locks
By JavaJuJitZu in forum Threads and SynchronizationReplies: 3Last Post: 02-19-2010, 02:47 AM -
A small free utility developed in SWT
By alexpkb in forum SWT / JFaceReplies: 0Last Post: 08-18-2009, 11:11 PM -
Thread Safe Methods / Locks
By mrhyman in forum Threads and SynchronizationReplies: 16Last Post: 10-24-2008, 09:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks