Results 1 to 2 of 2
  1. #1
    JSL5101 is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Synchronization utility to manage locks

    Hey folks,

    Say I have the following:

    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 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.

    I am trying to find something like:

    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.");
              }
         }
    }
    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.

    Since concurrent calls for the same ID need the same lock, the Utility class either:
    1. creates the lock, stores it in memory, then returns it
    2. 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:

    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
    }
    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.

    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.

  2. #2
    shnplr is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Hi,

    This approach doesn't work in a clustered environment (multiple servers/JVM's).

    Can you change your update statement to check for null?

    update my_table
    set val = :val
    where my_id = :id and val is null

Similar Threads

  1. Problems using wait() and notifyAll() with locks
    By cuffJ in forum Threads and Synchronization
    Replies: 6
    Last Post: 08-18-2010, 03:21 AM
  2. XML compare utility
    By JMayura in forum Advanced Java
    Replies: 0
    Last Post: 06-08-2010, 01:05 PM
  3. InterruptedException and Locks
    By JavaJuJitZu in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-19-2010, 02:47 AM
  4. A small free utility developed in SWT
    By alexpkb in forum SWT / JFace
    Replies: 0
    Last Post: 08-18-2009, 11:11 PM
  5. Thread Safe Methods / Locks
    By mrhyman in forum Threads and Synchronization
    Replies: 16
    Last Post: 10-24-2008, 09:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •