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.
@author Edward A. Lee, Mudit Goel, Lukito Muliadi, Xiaojun Liu
@version $Id: Workspace.java,v 1.108 2006/09/21 15:45:37 cxh Exp $
@since Ptolemy II 0.2
@Pt.ProposedRating Green (liuxj)
@Pt.AcceptedRating Green (liuxj)