Interface Condition - bind/unbind ?!

Doug Lea dl@cs.oswego.edu
Wed, 30 Jan 2002 20:05:36 -0500


Hi Alexander; I'm happy to see that you subscribed to this list!

> Are there any reasons why there is no interface to bind/unbind
> a condition to(another)/from a corresponding lock object?
> 
> http://groups.google.com/groups?as_umsgid=slrn9ehssd.hq.kaz%40cafe.net

The supplied implementations of Condition won't be rebindable.
The reasons are similar to the ones stated by some people in that
newsgroup thread: It reduces the likelihood of error and is more
efficient for the vastly most common case.

However, it is not hard to create your own Condition implementation
class that is rebindable. (Condition is an interface.)
You can write something like:

class RebindableCondition implements Condition {
  private Condition delegate;

  RebindableCondition(Object l) {
    delegate = Locks.createConditionFor(l);
  }

  void await() ... { delegate.await(); }
  // etc

  void rebind(Object l) {
    delegate = Locks.createConditionFor(l);
  }
}

Where I left out the necessary internal synchronization, and the error
checking that would be needed, both of which would take some thought.
Creating a new Condition for each rebind is more expensive than
resetting a field, but all in all, I think this is the right tradeoff.
It IS a rare (and rarely recomendable) usage, and even here, rebinds are
infrequent, so this strategy ought to be competitive.

Do you agree?

-- 
Doug Lea, Computer Science Department, SUNY Oswego, Oswego, NY 13126 USA
dl@cs.oswego.edu 315-312-2688 FAX:315-312-5424 http://gee.cs.oswego.edu/