G
Guest
Hi,
Here is the scenario. I have a list of IDs and there are multiple threads
trying to add/remove/read from this list. I can do in C#
1. create Hashtable hList = Hashtable.Synchronized(new Hashtable());
2. create ArrayList aList = ArrayList.Synchronized(new ArrayList());
3. create a string sList = "";
For 1 and 2, since the list is synced, many threads can directly
add/remvoe/read without worrying about the synchrionization.
For 3, I could just
lock(sList)
sList.Replace(id,"");
for removing.
lock(sList)
sList += id;
for addition.
lock(sList)
sList.Indexof(id) >= 0
for searching availability.
My question is
Which way is the thread-safe and the most efficient way to do my work?
If one method is preferred at certain list length, is there a breaking point
based on teh length of the list, by then one method may start to get better
than another one.
The situation is very simple but I face it in most of my applications. Very
interested on how MS implement it behind the scene and whehter any evaluation
is done on this.
Thanks a lot
Chris
Here is the scenario. I have a list of IDs and there are multiple threads
trying to add/remove/read from this list. I can do in C#
1. create Hashtable hList = Hashtable.Synchronized(new Hashtable());
2. create ArrayList aList = ArrayList.Synchronized(new ArrayList());
3. create a string sList = "";
For 1 and 2, since the list is synced, many threads can directly
add/remvoe/read without worrying about the synchrionization.
For 3, I could just
lock(sList)
sList.Replace(id,"");
for removing.
lock(sList)
sList += id;
for addition.
lock(sList)
sList.Indexof(id) >= 0
for searching availability.
My question is
Which way is the thread-safe and the most efficient way to do my work?
If one method is preferred at certain list length, is there a breaking point
based on teh length of the list, by then one method may start to get better
than another one.
The situation is very simple but I face it in most of my applications. Very
interested on how MS implement it behind the scene and whehter any evaluation
is done on this.
Thanks a lot
Chris