A moving monitor
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Dane_Foster
Posted On:   Saturday, February 2, 2002 10:39 AM

Lets say we have a class that caches some data into a String. A scenario of this would be a database lookup and the results are formatted as an XML string. If the data hasn't changed then it doesn't make sense to re-query and re-format, so you have the following lines of code: private String cache = ""; // other stuff is happening ..... // ...... 1: public String getDBcontent() 2: { 3: synchronized( this.cache ) 4: { 5: if( "" == this.cache ) 6: this.cache = resultsOfProcesssing(); 7: return this.cache; 8: } 9: } Now lets say that 2 threads simultaneously call 'g   More>>

Lets say we have a class that caches some data into a String. A scenario of this would be a database lookup and the results are formatted as an XML string. If the data hasn't changed then it doesn't make sense to re-query and re-format, so you have the following lines of code:

			
private String cache = "";

// other stuff is happening .....
// ......
1: public String getDBcontent()
2: {
3: synchronized( this.cache )
4: {
5: if( "" == this.cache )
6: this.cache = resultsOfProcesssing();
7: return this.cache;
8: }
9: }


Now lets say that 2 threads simultaneously call 'getDBcontent', this is the very first time that the method is called so 'cache' is still equal to an empty String (""). The Threads contend for the monitor of 'cache' and one wins (T1) while the other waits (T2). Because of the way the method is coded T1 will eventually change what 'cache' is pointing to while T2 is waiting. What I would like to know is, what is the deterministic behavior of T2 once T1 has changed cache's reference?



I'll tell what I think happens; Even though after the execution of line 6 'cache' is referencing a different object, T2 is still in the waiting queue of the original reference ("") and thus, after T1 exits the method via line 7 T2 can/will become runnable and attempt to acquire the monitor of the original reference (empty String). Upon acquiring the monitor it will execute line 5 --which evaluates to true-- and exits at line 7 returning the cached results.


As I've said before, this is what I _think_ happens, unfortunately I don't have a way of finding out for certain, so I'm hoping a Thread expert can tell me what's the deterministic behavior of a Thread given the above context. Thanx.    <<Less

Re: A moving monitor

Posted By:   Duncan_Forster  
Posted On:   Saturday, February 2, 2002 06:09 PM

You are correct but this should never be done. If you want to synchronize use something other than a string. Such as this or if thats to broad then create an object as a lock for the cache, eg:



private String cache = null;
private final Object cacheLock = new Object();

public String getDBcontent()
{
if( this.cache == null )
{
synchronized( CacheLock )
{
if( this.cache == null )
this.cache = resultsOfProcesssing();
}
}

return this.cache;
}
About | Sitemap | Contact