EDU.oswego.cs.dl.util.concurrent
Class SynchronizedVariable

java.lang.Object
  extended by EDU.oswego.cs.dl.util.concurrent.SynchronizedVariable
All Implemented Interfaces:
Executor
Direct Known Subclasses:
SynchronizedBoolean, SynchronizedByte, SynchronizedChar, SynchronizedDouble, SynchronizedFloat, SynchronizedInt, SynchronizedLong, SynchronizedRef, SynchronizedShort

public class SynchronizedVariable
extends java.lang.Object
implements Executor

Base class for simple, small classes maintaining single values that are always accessed and updated under synchronization. Since defining them for only some types seemed too arbitrary, they exist for all basic types, although it is hard to imagine uses for some.

These classes mainly exist so that you do not have to go to the trouble of writing your own miscellaneous classes and methods in situations including:

While they cannot, by nature, share much code, all of these classes work in the same way.

Construction
Synchronized variables are always constructed holding an initial value of the associated type. Constructors also establish the lock to use for all methods:

Update methods
Each class supports several kinds of update methods:

Guarded methods
All Waitable subclasses provide notifications on every value update, and support guarded methods of the form whenpredicate, that wait until the predicate hold, then optionally run any Runnable action within the lock, and then return. All types support:

(If the action argument is null, these return immediately after the predicate holds.) Numerical types also support The Waitable classes are not always spectacularly efficient since they provide notifications on all value changes. They are designed for use in contexts where either performance is not an overriding issue, or where nearly every update releases guarded waits anyway.

Other methods
This class implements Executor, and provides an execute method that runs the runnable within the lock.

All classes except SynchronizedRef and WaitableRef implement Cloneable and Comparable. Implementations of the corresponding methods either use default mechanics, or use methods that closely correspond to their java.lang analogs. SynchronizedRef does not implement any of these standard interfaces because there are many cases where it would not make sense. However, you can easily make simple subclasses that add the appropriate declarations.

[ Introduction to this package. ]


Field Summary
protected  java.lang.Object lock_
           
 
Constructor Summary
SynchronizedVariable()
          Create a SynchronizedVariable using itself as the lock
SynchronizedVariable(java.lang.Object lock)
          Create a SynchronizedVariable using the supplied lock
 
Method Summary
 void execute(java.lang.Runnable command)
          If current thread is not interrupted, execute the given command within this object's lock
 java.lang.Object getLock()
          Return the lock used for all synchronization for this object
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lock_

protected final java.lang.Object lock_
Constructor Detail

SynchronizedVariable

public SynchronizedVariable(java.lang.Object lock)
Create a SynchronizedVariable using the supplied lock


SynchronizedVariable

public SynchronizedVariable()
Create a SynchronizedVariable using itself as the lock

Method Detail

getLock

public java.lang.Object getLock()
Return the lock used for all synchronization for this object


execute

public void execute(java.lang.Runnable command)
             throws java.lang.InterruptedException
If current thread is not interrupted, execute the given command within this object's lock

Specified by:
execute in interface Executor
Throws:
java.lang.InterruptedException