5 Replies Latest reply: May 26, 2012 7:43 PM by jtahlborn RSS

    Is it possible to acquire "old" lock after recreating object?

    681840
      To separate the problem I am facing, please see below:
          private static Object  lockObject = new Object();
      
          public SomeClass() {
              super();
          }
      
      
          public void resetIn() {
              synchronized (lockObject) {
                     //Access common resource
              }
          }
      
          public String getStr() {
              synchronized (lockObject) {
                  //access common resource
                  return s;
              }
          }
          public void clear() {
              //This is the issue.
              lockObject =null;
          }
           public void recreateLockObject() {
              //This is the issue.
              lockObject = new Object();
          }
      }
      In bigger project, I have lockObject based on ID of some class. And to minimize the size of lockObjects, this has to be cleared at some stage (which I have shown in clear method).

      Problem is that once the lockObject is recreated after clear, the existing lock (in getStr or setStr is not respected in other method).

      Is there a way, I can pass on the locking state to new Object created.?
        • 1. Re: Is it possible to acquire "old" lock after recreating object?
          796440
          Nope, can't be done. You're basically saying, "Can I make this new object BE that other object?" There's one lock per object, and it is permanently bound to that object.

          However, this part makes no sense:
          And to minimize the size of lockObjects, this has to be cleared at some stage
          Why do you think you need to do this? And if you still need the same lock, then you can't get rid of that object. It's like you're saying, "I want to use less memory, so I'll get rid of this object, but now I need a new object, and I want it to be the same as the old one." Just keep the old one.
          • 2. Re: Is it possible to acquire "old" lock after recreating object?
            681840
            Thanks

            Then in this case, I tried to check if any lock is existing on the object or not, if Not then don't clear it.

            doing
            synchronized(lockObjet)
            {
              lockObject==null
            }
            didn't help.

            Any way, I can check that if any thread has already acquired lock on lockObject then do not recreate it?

            Edited by: A R on May 25, 2012 8:47 PM
            • 4. Re: Is it possible to acquire "old" lock after recreating object?
              796440
              A R wrote:
              Any way, I can check that if any thread has already acquired lock on lockObject then do not recreate it?
              http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/locks/Lock.html has tryLock() methods.
              http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/locks/ReentrantLock.html has isLocked() and isHeldByCurrentThread() and getHoldCount() and getOwner().

              However, it's still not clear what you're actually trying to do, nor that it makes sense to do whatever it is.
              • 5. Re: Is it possible to acquire "old" lock after recreating object?
                jtahlborn
                if i understand correctly what you are saying, i think you are saying that you get an object based an ID and you use that to lock on. however, this object also has a non-trivial amount of state associate with it, and at some point in time that object is discarded in order to free up memory. if that is the case, what you should do is separate the lock from the state.

                e.g.:
                public class LockObject
                {
                  private Object _state;
                
                  public synchronized void setState(Object newState) { _state = newState; }
                }
                
                    private static final LockObject lockObject = new LockObject();
                 
                    public SomeClass() {
                        super();
                    }
                 
                 
                    public void resetIn() {
                        synchronized (lockObject) {
                               //Access common resource
                        }
                    }
                 
                    public String getStr() {
                        synchronized (lockObject) {
                            //access common resource
                            return s;
                        }
                    }
                    public void clear() {
                        lockObject.setState(null);
                    }
                     public void recreateLockObjectState() {
                        lockObject.setState(new Object());
                    }
                }