My tech blog and other writing

Friday, May 05, 2006

Thread saftey

I have a collection in an object that has a DirectoryWatcher and attaches to it's events. The collection will get added to and subtracted from based on the event from the directory watcher... also on a special occasion I may iterate through the collection.

Problem... the collection (Hashtable) I am using is not threadsafe... and if I use the Synchronized method to get a threadsafe version, it is only threadsafe for atomic operations... (ie... if you do a read then a write it synchronizes the operations so they happen in order (not at the same time)... but somebody could sneak in between your read and write)...

Solution... lock on the Hashtable synchroot... Since the only lock is on the private hashtable from this object itself I am not concerned about deadlocking posibilities...

0 Comments:

Post a Comment

<< Home