Best way to synchronize collection?
-
Hi! I would like to know what's the best way to accomplish sync. in an arraylist when using multiple threads. I' m using "synclock Coll.SyncRoot" and then "End synclock" (duh). Is this method rather "strong" for it blocks other threads that also use this? And what about if i use the synchronized method and the IsSynchronized property, is this approach better than the last case? Another question is i read in the help doc. that " Hashtable can safely support one writer and multiple readers concurrently". Won't the write affect enumerations done by the readers? Thanks in advance.
-
Hi! I would like to know what's the best way to accomplish sync. in an arraylist when using multiple threads. I' m using "synclock Coll.SyncRoot" and then "End synclock" (duh). Is this method rather "strong" for it blocks other threads that also use this? And what about if i use the synchronized method and the IsSynchronized property, is this approach better than the last case? Another question is i read in the help doc. that " Hashtable can safely support one writer and multiple readers concurrently". Won't the write affect enumerations done by the readers? Thanks in advance.
carlos_rocha wrote: I' m using "synclock Coll.SyncRoot" and then "End synclock" (duh). Is this method rather "strong" for it blocks other threads that also use this? It's supposed to block other threads. That's the whole point behind synchronized access. carlos_rocha wrote: that " Hashtable can safely support one writer and multiple readers concurrently". Won't the write affect enumerations done by the readers? If you keep reading the doc's, you'll see that enumeration is intrinsically not a thread-safe operation. This is because the collection can be modified while the eneration is taking place, and hence, can corrupt the enumeration process. If your going to enumerate the collection, lock it on SyncRoot first, preventing all other threads from doing anything, including writes, with the collection, though, I think you can figure out the pitfalls of doing this... If you want a synchronized collection that supports one writer and multiple readers simultaneously, use the
.Synchronized()
method to return a thread synchronized wrapper around your Hashtable and use this version of your Hashtable for all accesses. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -
carlos_rocha wrote: I' m using "synclock Coll.SyncRoot" and then "End synclock" (duh). Is this method rather "strong" for it blocks other threads that also use this? It's supposed to block other threads. That's the whole point behind synchronized access. carlos_rocha wrote: that " Hashtable can safely support one writer and multiple readers concurrently". Won't the write affect enumerations done by the readers? If you keep reading the doc's, you'll see that enumeration is intrinsically not a thread-safe operation. This is because the collection can be modified while the eneration is taking place, and hence, can corrupt the enumeration process. If your going to enumerate the collection, lock it on SyncRoot first, preventing all other threads from doing anything, including writes, with the collection, though, I think you can figure out the pitfalls of doing this... If you want a synchronized collection that supports one writer and multiple readers simultaneously, use the
.Synchronized()
method to return a thread synchronized wrapper around your Hashtable and use this version of your Hashtable for all accesses. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming GnomeHi! Thanks for your reply.The thing is that i have several threads but only two of them are acessing the arraylist, which contains info on the "working" threads.One of these two is like a monitor thread that checks every once in a while for exited threads and then removes an item from the arraylist.At the same time the other main thread in also reading the list and as new threads are created another node is added in the arraylist.So, only these two threads manipulate the arraylist.What's best? Use syncLock or .synchronized() ? Thanks in advance.
-
Hi! Thanks for your reply.The thing is that i have several threads but only two of them are acessing the arraylist, which contains info on the "working" threads.One of these two is like a monitor thread that checks every once in a while for exited threads and then removes an item from the arraylist.At the same time the other main thread in also reading the list and as new threads are created another node is added in the arraylist.So, only these two threads manipulate the arraylist.What's best? Use syncLock or .synchronized() ? Thanks in advance.
It really doesn't matter, based on the requirements you just gave. If that's all you're doing and one of those threads is just a lazy garbage collector, then there will be no performance issues with using either method. If it were up to me, I'd probably use the .Synchronized() method just to save myself from writing my own synchronization management code. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
It really doesn't matter, based on the requirements you just gave. If that's all you're doing and one of those threads is just a lazy garbage collector, then there will be no performance issues with using either method. If it were up to me, I'd probably use the .Synchronized() method just to save myself from writing my own synchronization management code. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
OK.Thanks.I've been reading the help and i came upon the readerwriterLock Class.It says that it's suitable for various reads and infrequent writes, instead of blocking the code for the other thread, they both could acess the hash Table(or arrayList)...Any comments?Please... Thanks in advance. Never say never