|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Is this static method threadsafe?Following is a static method. Can you please tell me if this is threadsafe...why? If it is not threadsafe...why? Thank you. public static string GetCachedApplicationWideObject(string HashtableKey) { //Cache["DefaultSampleQPage"] holds the hashtable if(Cache["DefaultSampleQPage"]==null) { CacheApplicationWideObjects(); _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]); return ((string)HTable[HashtableKey]); } else { _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]); return ((string)HTable[HashtableKey]); } } I forgot to add this information:
_sHTable is a static variable which is like a const variable for the entire application; so no problem if two threads access that information at the same time. But my only question is if the parameter 1 is passed by thread 1 and parameter 2 is passed by thread 2 is there any possibility that thread 1 will be returned thread 2's result and vice versa? Thanks Show quoteHide quote "Diffident" wrote: > Hello All, > > Following is a static method. Can you please tell me if this is > threadsafe...why? If it is not threadsafe...why? > > Thank you. > > public static string GetCachedApplicationWideObject(string HashtableKey) > { > //Cache["DefaultSampleQPage"] holds the hashtable > if(Cache["DefaultSampleQPage"]==null) > { > CacheApplicationWideObjects(); > _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]); > return ((string)HTable[HashtableKey]); > } > else > { > _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]); > return ((string)HTable[HashtableKey]); > } > } no its not threadsafe. you do not serialize access to the hashtable, so race
condiditons can occur. also two threads can run CacheApplicationWideObjects at the same time (is it threadsafe?) the treading issue you worried about should not be a problem if CacheApplicationWideObjects is threadsafe. -- bruce (sqlwork.com) Show quoteHide quote "Diffident" <Diffid***@discussions.microsoft.com> wrote in message news:26E5BDB9-24C6-48D9-9B77-C0CE81B6E380@microsoft.com... >I forgot to add this information: > > _sHTable is a static variable which is like a const variable for the > entire > application; so no problem if two threads access that information at the > same > time. > > But my only question is if the parameter 1 is passed by thread 1 and > parameter 2 is passed by thread 2 is there any possibility that thread 1 > will > be returned thread 2's result and vice versa? > > Thanks > > "Diffident" wrote: > >> Hello All, >> >> Following is a static method. Can you please tell me if this is >> threadsafe...why? If it is not threadsafe...why? >> >> Thank you. >> >> public static string GetCachedApplicationWideObject(string HashtableKey) >> { >> //Cache["DefaultSampleQPage"] holds the hashtable >> if(Cache["DefaultSampleQPage"]==null) >> { >> CacheApplicationWideObjects(); >> _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]); >> return ((string)HTable[HashtableKey]); >> } >> else >> { >> _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]); >> return ((string)HTable[HashtableKey]); >> } >> } If all you are ever doing is reading from the Cache, i fail to see where the
problem lies, because the data will always be the same. It's when you might have another thread that attempts to WRITE to the same Cache item that you could run into issues. Take a look at the lock keyword. Better yet, read MVP Jon Skeet's articles on the subject: http://www.yoda.arachsys.com/csharp/threads/ Peter -- Show quoteHide quoteCo-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com "Diffident" wrote: > Hello All, > > Following is a static method. Can you please tell me if this is > threadsafe...why? If it is not threadsafe...why? > > Thank you. > > public static string GetCachedApplicationWideObject(string HashtableKey) > { > //Cache["DefaultSampleQPage"] holds the hashtable > if(Cache["DefaultSampleQPage"]==null) > { > CacheApplicationWideObjects(); > _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]); > return ((string)HTable[HashtableKey]); > } > else > { > _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]); > return ((string)HTable[HashtableKey]); > } > }
Other interesting topics
Images inline with blog contents - HOW??
Advantages of Static Vs Instance methods? Use validationSummary to display error message in user control CompareValidator for dates > than today VWD 2005 Express: "copy web site" creates multiple publishform (2.0) Trap the Maximum request length this should be easy... Download multiplefiles session values What is better for a web page???? |
|||||||||||||||||||||||