[concurrency-interest] Protecting sensitive resources
mike.quilleash at azuresolutions.com
Mon May 8 12:05:22 EDT 2006
I have a class that is responsible for delegating resources to other
Snapshot of the code below...
initalise() handles refreshing the internal state (other internal
variables which are not shown).
openSession() should create a new session and return it (unless
releaseSession() should return a session to the class and mark it as no
The requirements for how this class should work is...
The class should keep track of current in-use session objects
(UsageSession) which must be threadsafe.
UsageSessions must be returned to the class when they are no longer
The class may be reinitialised at any time in which case:
It should not refresh until all Sessions have been returned.
It should not allow any new sessions to be delegated out until
reinitialise is complete.
I'd appreciate any feedback or problems anyone sees with the
implementation below, I've stared at it for a while now and run through
the different possible scenarios of thread states etc but can't see any
// set of currently open sessions
private Set< UsageSession > openSessions;
// locks and conditions protecting the open sessions list
private Lock openSessionsLock = new ReentrantLock();
private Condition openSessionsEmptyCondition =
private Condition openSessionsNotRefreshingCondition =
private volatile boolean refreshing = false;
// set refreshing flag
refreshing = true;
// wait for the open sessions list to be empty
while( openSessions.size() > 0 )
// clear out the open sessions - will already be empty
openSessions = new HashSet< UsageSession >();
// TODO: do initialisation here
// clear refreshing flag
refreshing = false;
// signal all threads waiting on this condition
// open a usage session for use
public UsageSession openSession()
// lock on the open sessions list
// if refreshing then wait for the refreshing condition
while ( refreshing )
// wait on the not refreshing condition
UsageSession usageSession = new UsageSession( this );
openSessions.add( usageSession );
// call back made from the usage session when it is closed
void releaseSession( UsageSession usageSession )
if ( !openSessions.remove( usageSession ) )
throw new IllegalArgumentException( "Attempted to
release unknown usage session" );
// signal that the open sessions set is empty incase
something is waiting
if ( openSessions.isEmpty() )
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Concurrency-interest