net.sf.ivmaidns.util
Interface Lockable


public interface Lockable

Interface for objects synchronized on lock. A class (typically active) implementing this interface allows an application at any time to choose any custom object on which the instance of the class should perform all its synchronization. This is very useful when several stand-alone active objects (with an internal self-synchronization) need to work in a particular synchronous way (without any possibility of dead-lock).

Since:
2.0
Version:
2.0
Author:
Ivan Maidanski
See Also:
ActivityCore

Method Summary
 java.lang.Object getLock()
          Just returns the current lock object.
 void setLock(java.lang.Object newLock)
          Sets new lock object to be used for synchronization.
 

Method Detail

setLock

void setLock(java.lang.Object newLock)
             throws java.lang.NullPointerException
Sets new lock object to be used for synchronization. Important notes: inside the class (implementing this interface), lock should be protected transient and non-null, by default lock is set to this (during initialization, deserialization or cloning); all needed synchronization (inside/outside the class) must be done on lock (as shown below). This method should be implemented as follows:
 protected transient Object lock = this;
 public final void setLock(Object newLock)
 throws NullPointerException
 { // note: take care on deserializing
 Object curLock;
 newLock.equals(newLock); // this is a check for null
 do
 {
 synchronized (curLock = lock)
 {
 if (curLock == lock)
 { // check that synchronized on this lock
 lock = newLock; // this is a synchronized operation
 break;
 }
 }
 } while (true);
 }
 

Parameters:
newLock - a lock object (must be non-null) to be used by this object for doing all its synchronization.
Throws:
java.lang.NullPointerException - if newLock is null.
See Also:
getLock()

getLock

java.lang.Object getLock()
Just returns the current lock object. This method should be implemented as follows:
 public final Object getLock()
 {
 return lock;
 }
 

Returns:
the current lock object, on which this object performs all its synchronization.
See Also:
setLock(java.lang.Object)