关于散列表,arraylist,字符串和同步的效率 [英] Efficiency about hashtable, arraylist, string and synchronization
问题描述
这是一个场景。我有一个ID列表,并且有多个线程
尝试添加/删除/读取此列表。我可以用C#
1.创建Hashtable hList = Hashtable.Synchronized(new Hashtable());
2.创建ArrayList aList = ArrayList.Synchronized (new ArrayList());
3.创建一个字符串sList ="" ;;
对于1和2,因为列表已同步,许多线程可以直接
add / remvoe / read而不用担心同步化。
对于3,我可以只是
lock(sList)
sList.Replace(id,"");
用于删除。
lock(sList)
sList + = id;
添加。
lock(sList)
sList.Indexof(id) > = 0
用于搜索可用性。
我的问题是
哪种方式是线程安全的以及最有效的工作方式吗?
如果在某个列表长度上首选一种方法,是否有一个断点
基于长度列表,然后一种方法可能会开始变得更好
比另一个。
情况非常简单,但我在大多数应用程序中都面对它。非常
感兴趣的是MS如何在幕后实施它并且对此进行评估
就此完成了。
非常感谢
克里斯
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
推荐答案
chrisben写道:
chrisben wrote:
这是场景。我有一个ID列表,并且有多个线程
尝试添加/删除/读取此列表。我可以用C#
1.创建Hashtable hList = Hashtable.Synchronized(new Hashtable());
2.创建ArrayList aList = ArrayList.Synchronized (new ArrayList());
3.创建一个字符串sList ="" ;;
对于1和2,因为列表已同步,许多线程可以直接添加/删除/读取
而不用担心同步化。
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.
只要你不尝试遍历列表/表格......只有
*个别*操作是同步的。
So long as you don''t try to iterate through the list/table... only
*individual* operations are synchronized.
3,我可以
锁定(sList)
sList.Replace(id," ;");
用于删除。
For 3, I could just
lock(sList)
sList.Replace(id,"");
for removing.
这将是一个无操作 - string.Replace返回一个新字符串。
That would be a no-op - string.Replace returns a new string.
lock( sList)
sList + = id;
添加。
lock(sList)
sList += id;
for addition.
这会更改sList以引用不同的字符串。
This changes sList to refer to a different string.
lock(sList)
sList.Indexof(id)> = 0
用于搜索可用性。
我的问题是
哪种方式是线程安全且最有效的方式来完成我的工作?
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?
根本不要使用锁定字符串。它们本身就是线程安全的,
但是你需要知道任何改变的东西。字符串
实际上并没有改变字符串 - 它只是返回一个新字符串。
Don''t use locking at all for strings. They''re threadsafe in themselves,
but you need to be aware that anything which "changes" the string
doesn''t actually change the string - it just retuns a new string.
如果在某些列表中首选一种方法长度,是否有一个断点
基于列表的长度,然后一种方法可能开始变得更好
比另一种方法。
情况非常简单,但我在大多数应用程序中面对它。非常
对MS如何在幕后实现它感兴趣,并且对此进行任何评估
。
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.
如果你*实际上*想要同步*变量*
sList上的操作,你应该创建一个单独的锁对象,并锁定在那个
,只要你想改变sList'的值。
Jon
If you *actually* want to synchronize operations on the *variable*
sList, you should create a separate lock object, and lock on that
whenever you want to change sList''s value.
Jon
Hashtable,但不是你的方式;-p
字符串选项不好。不要靠近它。首先你需要一个
分隔符,但主要是因为字符串是不可变的(因此你的代码实际上是错误的
)。你需要sList = sList.Replace(id,"");但现在你
有多个锁定对象;好吧,你可以通过一个单独的锁来避免这种情况,
但是你仍在制作*很多*的字符串。
就个人而言,我也不会靠近.Synchronized选项......我只会:
lock(hList){
hList.Add(id);
}
等;这样你就知道了锁的持续时间,所以你可以安全地做多个步骤作为原子单位:
lock(hList){
if(!hList.Contains(id)){
hList.Add(id,id); //值是假的
}
}
还要注意(假设ID是一个整数)这将涉及拳击;如果你可以使用
,我会建议转到2.0并使用字典< int,int> ;.
Marc
Hashtable, but not your way ;-p
The string option is bad. Don''t go near it. Firstly you would need a
delimiter, but mainly because strings are immutable (and hence your code is
actually wrong). You would need sList = sList.Replace(id,""); but now you
have multiple lock objects; OK you could avoid this with a separate lock,
but you are still making *lots* of strings.
Personally I also wouldn''t go near the .Synchronized options... I''d just:
lock(hList) {
hList.Add(id);
}
etc; this way you know the duration of the lock, so you can safely do
multiple steps as an atomic unit:
lock(hList) {
if(!hList.Contains(id)) {
hList.Add(id,id); // value is dummy
}
}
Note also that (assuming ID is an integer) this will involve boxing; if you
can, I would suggest moving to 2.0 and using a Dictionary<int,int>.
Marc
谢谢Jon。实际上,我在我的
应用程序中使用了arraylist和hashtable,但现在我认为它可能效率较低,特别是如果我的列表
很小。
我错了字符串示例,应该像
lock(myObject)
sList = sList.Replace(id," ");
lock(myObject)
sList + = id;
sList.Indexob(id )0(我应该锁定吗?)
我的感觉是,对于一个简短的列表,效率将是
string ArrayList Hashtable
但我不确定。
你怎么看?
谢谢
Chris
" Jon Skeet [C#MVP]"写道:
Thanks Jon. Actually, I used either arraylist and hashtable in my
applications, but now I think it may be less efficient especially if my list
is small.
I am wrong on string sample, should be like
lock(myObject)
sList = sList.Replace(id,"");
lock(myObject)
sList += id;
sList.Indexob(id) 0 (should I lock here?)
My feeling is that for a short list, efficiency will be
string ArrayList Hashtable
but I am not sure.
What do you think?
Thanks
Chris
"Jon Skeet [C# MVP]" wrote:
chrisben写道:
chrisben wrote:
这是一个场景。我有一个ID列表,并且有多个线程
尝试添加/删除/读取此列表。我可以用C#
1.创建Hashtable hList = Hashtable.Synchronized(new Hashtable());
2.创建ArrayList aList = ArrayList.Synchronized (new ArrayList());
3.创建一个字符串sList ="" ;;
对于1和2,因为列表已同步,许多线程可以直接添加/删除/读取
而不用担心同步化。
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.
只要你不试图遍历列表/表格......只有
*个别*操作是同步的。
So long as you don''t try to iterate through the list/table... only
*individual* operations are synchronized.
3,我可以
锁定(sList)
sList.Replace(id," ;");
用于删除。
For 3, I could just
lock(sList)
sList.Replace(id,"");
for removing.
这将是一个无操作 - string.Replace返回一个新字符串。
That would be a no-op - string.Replace returns a new string.
lock( sList)
sList + = id;
添加。
lock(sList)
sList += id;
for addition.
这会更改sList以引用不同的字符串。
This changes sList to refer to a different string.
lock(sList)
sList.Indexof(id)> = 0
用于搜索可用性。
我的问题是
哪种方式是线程安全且最有效的方式来完成我的工作?
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?
根本不要使用锁定字符串。它们本身就是线程安全的,
但是你需要知道任何改变的东西。字符串
实际上并没有改变字符串 - 它只是返回一个新字符串。
Don''t use locking at all for strings. They''re threadsafe in themselves,
but you need to be aware that anything which "changes" the string
doesn''t actually change the string - it just retuns a new string.
如果在某些列表中首选一种方法长度,是否有一个断点
基于列表的长度,然后一种方法可能开始变得更好
比另一种方法。
情况非常简单,但我在大多数应用程序中面对它。非常
对MS如何在幕后实现它感兴趣,并且对此进行任何评估
。
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.
如果你*实际上*想要同步*变量*
sList上的操作,你应该创建一个单独的锁对象,并锁定在那个
,只要你想改变sList的价值。
Jon
If you *actually* want to synchronize operations on the *variable*
sList, you should create a separate lock object, and lock on that
whenever you want to change sList''s value.
Jon
这篇关于关于散列表,arraylist,字符串和同步的效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!