monitor
Class AbstractMonitor

java.lang.Object
  extended by monitor.AbstractMonitor
Direct Known Subclasses:
Monitor

public abstract class AbstractMonitor
extends java.lang.Object

A class for Monitors. Monitors provide coordination of concurrent threads. Each Monitor protects some resource, usually data. At each point in time a monitor object is occupied by at most one thread.

Use this class as a base class if you can.

Use class Monitor if you are already extending another class.

See
C.A.R. Hoare, 'Monitors: an operating system structuring concept', Communications of the ACM 17, Oct. 1974, pp. 549-557. Reprinted in C.A.R.Hoare and C.B. Jones, Essay's in Computing Science, Prentice-Hall, 1989.
and
P. Brinch Hansen, The programming language Concurrent Pascal, IEEE Transactions on Software Engineering 1, June 1975, 199-207.

The details of pre- and post-conditions for signal and await and the idea to use a count function come from Ole-Johan Dahl, 'Monitors revisited', in A Classical Mind, A. W. Roscoe (ed.), 1994.

Version:
2.0
Author:
Theodore S. Norvell
See Also:
Condition, Monitor

Constructor Summary
protected AbstractMonitor()
           
protected AbstractMonitor(java.lang.String name)
           
 
Method Summary
 void addListener(MonitorListener newListener)
          Register a listener
protected  void doWithin(java.lang.Runnable runnable)
          Run the runnable inside the monitor.
protected
<T> T
doWithin(RunnableWithResult<T> runnable)
          Run the runnable inside the monitor.
protected  void enter()
          Enter the monitor.
 java.lang.String getName()
           
protected  boolean invariant()
          The invariant.
protected  void leave()
          Leave the monitor.
protected
<T> T
leave(T result)
          Leave the monitor.
protected  Condition makeCondition()
          Create a condition queue with no associated checked Assertion.
protected  Condition makeCondition(Assertion prop)
          Create a condition queue associated with a checked Assertion.
protected  Condition makeCondition(java.lang.String name)
          Create a condition queue with no associated checked Assertion.
protected  Condition makeCondition(java.lang.String name, Assertion prop)
          Create a condition queue associated with a checked Assertion.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractMonitor

protected AbstractMonitor()

AbstractMonitor

protected AbstractMonitor(java.lang.String name)
Method Detail

getName

public java.lang.String getName()

invariant

protected boolean invariant()
The invariant. The default implementation always returns true. This method should be overridden if at all possible with the strongest economically evaluable invariant.


enter

protected void enter()
Enter the monitor. Any thread calling this method is delayed until the monitor is unoccupied. Upon returning from this method, the monitor is considered occupied.

A thread must not attempt to enter a Monitor it is already in.


leave

protected void leave()
Leave the monitor. After returning from this method, the thread no longer occupies the monitor.

Only a thread that is in the monitor may leave it.

Throws:
java.lang.AssertionError - if the thread that leaves is not the occupant.

leave

protected <T> T leave(T result)
Leave the monitor. After returning from this method, the thread no longer occupies the monitor.

Only a thread that is in the monitor may leave it.

Throws:
java.lang.AssertionError - if the thread that leaves is not the occupant.

doWithin

protected void doWithin(java.lang.Runnable runnable)
Run the runnable inside the monitor.

Any thread calling this method will be delayed until the monitor is empty. The "run" method of its argument is then executed within the protection of the monitor.

When the run method returns, if the thread still occupies the monitor, it leaves the monitor.

Parameters:
runnable - A Runnable object.

doWithin

protected <T> T doWithin(RunnableWithResult<T> runnable)
Run the runnable inside the monitor.

Any thread calling this method will be delayed until the monitor is empty. The "run" method of its argument is then executed within the protection of the monitor.

When the run method returns, if the thread still occupies the monitor, it leaves the monitor.

Thus the signalAndLeave method may be called within the run method.

Parameters:
runnable - A RunnableWithResult object.
Returns:
The value computed by the run method of the runnable.

makeCondition

protected Condition makeCondition(Assertion prop)
Create a condition queue associated with a checked Assertion. The Assertion will be checked prior to an signal of the condition.


makeCondition

protected Condition makeCondition()
Create a condition queue with no associated checked Assertion.


makeCondition

protected Condition makeCondition(java.lang.String name,
                                  Assertion prop)
Create a condition queue associated with a checked Assertion. The Assertion will be checked prior to an signal of the condition.


makeCondition

protected Condition makeCondition(java.lang.String name)
Create a condition queue with no associated checked Assertion.


addListener

public void addListener(MonitorListener newListener)
Register a listener