/* * @(#) Lockable.java - Interface for objects synchronized on lock. * (c) 2000 Ivan Maidanski http://ivmai.chat.ru * Freeware class library sources. All rights reserved. ** * Language: Java [pure] * Tested with: JDK v1.1.6 * Last modified: 2000-12-13 14:50:00 GMT+03:00 */ /* * This software is the proprietary information of the author. ** * Permission to use, copy, and distribute this software and its * documentation for non-commercial purposes and without fee is * hereby granted provided that this copyright notice appears in all * copies. ** * This software should not be modified in any way; any found bug * should be reported to the author. ** * The author disclaims all warranties with regard to this software, * including all implied warranties of merchantability and fitness. * In no event shall the author be liable for any special, indirect * or consequential damages or any damages whatsoever resulting from * loss of use, data or profits, whether in an action of contract, * negligence or other tortuous action, arising out of or in * connection with the use or performance of this software. */ package ivmai.util; /** * 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). ** * @see ActivityCore ** * @version 2.0 * @author Ivan Maidanski ** * @since 2.0 */ public interface Lockable { /** * 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);
 * }
 * 
** * @param newLock * a lock object (must be non-null) to be used by * this object for doing all its synchronization. * @exception NullPointerException * if newLock is null. ** * @see #getLock() */ public abstract void setLock(Object newLock) throws NullPointerException; /** * Just returns the current lock object. ** * This method should be implemented as follows: ** *
 * public final Object getLock()
 * {
 * return lock;
 * }
 * 
** * @return * the current lock object, on which this * object performs all its synchronization. ** * @see #setLock(java.lang.Object) */ public abstract Object getLock(); }