|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectptolemy.kernel.util.Workspace
public final class Workspace
An instance of Workspace is used for synchronization and version tracking of interdependent groups of objects. These objects are said to be in the workspace. This is not the same as the container association in Ptolemy II. A workspace is never returned by a getContainer() method.
The workspace provides a rudimentary directory service that can be used to keep track of the objects within it. It is not required to use it in order to use the workspace for synchronization. Items are added to the directory by calling add(). The names of the items in the directory are not required to be unique.
The synchronization model of the workspace is a multiple-reader, single-writer model. Any number of threads can simultaneously read the workspace. Only one thread at a time can have write access to the workspace, and while the write access is held, no other thread can get read access.
When reading the state of objects in the workspace, a thread must ensure that no other thread is simultaneously modifying the objects in the workspace. To read-synchronize on a workspace, use the following code:
try {
_workspace.getReadAccess();
// ... code that reads
} finally {
_workspace.doneReading();
}
We assume that the _workspace variable references the workspace, as for example
in the NamedObj class. The getReadAccess() method suspends the current thread
if another thread is currently modifying the workspace, and otherwise
returns immediately. Note that multiple readers can simultaneously have
read access. The finally clause is executed even if
an exception occurs. This is essential because without the call
to doneReading(), the workspace will never again allow any thread
to modify it.
To make safe changes to the objects in a workspace, a thread must write-synchronize using the following code:
try {
_workspace.getWriteAccess();
// ... code that writes
} finally {
_workspace.doneWriting();
}
Again, the call to doneWriting() is essential, or the workspace
will remain permanently locked to either reading or writing.
Note that it is not necessary to obtain a write lock just to add an item to the workspace directory. The methods for accessing the directory are all synchronized, so there is no risk of any thread reading an inconsistent state.
| Green (liuxj) |
| Green (liuxj) |
| Constructor Summary | |
|---|---|
Workspace()
Create a workspace with an empty string as its name. |
|
Workspace(java.lang.String name)
Create a workspace with the specified name. |
|
| Method Summary | |
|---|---|
protected java.lang.String |
_description(int detail,
int indent,
int bracket)
Return a description of the workspace. |
void |
add(NamedObj item)
Add an item to the directory. |
java.lang.String |
description()
Return a full description of the workspace and everything in its directory. |
java.lang.String |
description(int detail)
Return a description of the workspace. |
java.util.Enumeration |
directory()
Deprecated. Use directoryList() instead. |
java.util.List |
directoryList()
Return an unmodifiable list of the items in the directory, in the order in which they were added. |
void |
doneReading()
Indicate that the calling thread is finished reading. |
void |
doneWriting()
Indicate that the calling thread is finished writing. |
NamedObj |
getContainer()
Get the container. |
java.lang.String |
getDisplayName()
Return a name to present to the user, which is the same as what is returned by getName(). |
java.lang.String |
getFullName()
Get the full name. |
java.lang.String |
getName()
Get the name. |
java.lang.String |
getName(NamedObj relativeTo)
Get the name. |
void |
getReadAccess()
Obtain permission to read objects in the workspace. |
long |
getVersion()
Get the version number. |
void |
getWriteAccess()
Obtain permission to write to objects in the workspace. |
boolean |
handleModelError(NamedObj context,
IllegalActionException exception)
Handle a model error by throwing the specified exception. |
void |
incrVersion()
Increment the version number by one. |
void |
remove(NamedObj item)
Remove the specified item from the directory. |
void |
removeAll()
Remove all items from the directory. |
void |
setName(java.lang.String name)
Set or change the name. |
java.lang.String |
toString()
Return a concise description of the object. |
void |
wait(java.lang.Object obj)
Release all the read accesses held by the current thread and suspend the thread by calling Object.wait() on the specified object. |
void |
wait(java.lang.Object obj,
long timeout)
This method is equivalent to the single argument version except that you can specify a timeout, which is in milliseconds. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public Workspace()
public Workspace(java.lang.String name)
name - Name of the workspace.| Method Detail |
|---|
public void add(NamedObj item)
throws IllegalActionException
item - Item to list in the directory.
IllegalActionException - If the item has a container, is
already in the directory, or is not in this workspace.public java.lang.String description()
description in interface Nameablepublic java.lang.String description(int detail)
detail - The level of detail.
public java.util.Enumeration directory()
public java.util.List directoryList()
public final void doneReading()
InvalidStateException - If this method is called
before a corresponding call to getReadAccess() by the same thread.public final void doneWriting()
InvalidStateException - If this method is called before
a corresponding call to getWriteAccess() by the same thread.public NamedObj getContainer()
getContainer in interface Nameablepublic java.lang.String getDisplayName()
getDisplayName in interface NameablegetName()public java.lang.String getFullName()
getFullName in interface Nameablepublic java.lang.String getName()
getName in interface NameablesetName(String)public java.lang.String getName(NamedObj relativeTo)
getName in interface NameablerelativeTo - This argument is ignored.
setName(String)public final void getReadAccess()
InternalErrorException - If the calling thread is interrupted
while waiting to get read access.doneReading()public final long getVersion()
public final void getWriteAccess()
InternalErrorException - If the calling thread is interrupted
while waiting to get write access.doneWriting()
public boolean handleModelError(NamedObj context,
IllegalActionException exception)
throws IllegalActionException
context - The object in which the error occurred.exception - An exception that represents the error.
IllegalActionException - The exception passed
as an argument is always thrown.public final void incrVersion()
public void remove(NamedObj item)
item - The NamedObj to be removed.public void removeAll()
public void setName(java.lang.String name)
setName in interface Nameablename - The new name.getName()public java.lang.String toString()
toString in class java.lang.Object
public void wait(java.lang.Object obj)
throws java.lang.InterruptedException
obj - The object that the thread wants to wait on.
java.lang.InterruptedException - If the calling thread is interrupted
while waiting on the specified object and all the read accesses held
earlier by the thread are re-acquired.
InternalErrorException - If re-acquiring the read accesses
held earlier by the thread fails.
public void wait(java.lang.Object obj,
long timeout)
throws java.lang.InterruptedException
obj - The object that the thread wants to wait on.timeout - The maximum amount of time to wait, in milliseconds,
or zero to not specify a timeout.
java.lang.InterruptedException - If the calling thread is interrupted
while waiting on the specified object and all the read accesses held
earlier by the thread are re-acquired.
InternalErrorException - If re-acquiring the read accesses
held earlier by the thread fails.wait(Object)
protected java.lang.String _description(int detail,
int indent,
int bracket)
detail - The level of detail.indent - The amount of indenting.bracket - The number of surrounding brackets (0, 1, or 2).
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||