谁见过hashtable.count =-1的情况?呵呵。。我碰到了,不解?

fjnetphp 2011-01-11 03:16:38
谁见过hashtable.count =-1的情况?呵呵。。我碰到了,不解?
...全文
131 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
加油馒头 2011-01-11
  • 打赏
  • 举报
回复
你只是传说 会扔给你异常的
archu 2011-01-11
  • 打赏
  • 举报
回复
在多线程并发的情况下,这种问题当然有可能发生。
先看一下MSDN: http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx
上面关于线程安全有这么一段话
Thread Safety
--------------------------------------------------------------------------------

Hashtable is thread safe for use by multiple reader threads and a single writing thread. It is thread safe for multi-thread use when only one of the threads perform write (update) operations, which allows for lock-free reads provided that the writers are serialized to the Hashtable. To support multiple writers all operations on the Hashtable must be done through the wrapper returned by the Synchronized method, provided that there are no threads reading the Hashtable object.

Enumerating through a collection is intrinsically not a thread safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.


再来看一下反编译出来的代码


public virtual void Remove(object key)
{
uint num;
uint num2;
Hashtable.bucket bucket;
if (key == null)
{
throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key"));
}
uint num3 = this.InitHash(key, this.buckets.Length, out num, out num2);
int num4 = 0;
int index = (int) (num % this.buckets.Length);
Label_003A:
bucket = this.buckets[index];
if (((bucket.hash_coll & 0x7fffffff) == num3) && this.KeyEquals(bucket.key, key))
{
Thread.BeginCriticalRegion();
this.isWriterInProgress = true;
this.buckets[index].hash_coll &= -2147483648;
if (this.buckets[index].hash_coll != 0)
{
this.buckets[index].key = this.buckets;
}
else
{
this.buckets[index].key = null;
}
this.buckets[index].val = null;
this.count--;
this.UpdateVersion();
this.isWriterInProgress = false;
Thread.EndCriticalRegion();
}
else
{
index = (int) ((index + num2) % ((ulong) this.buckets.Length));
if ((bucket.hash_coll < 0) && (++num4 < this.buckets.Length))
{
goto Label_003A;
}
}
}



如果里面只有一个元素的时候,同时有两个线程调用Remove()方法,count是不是就有可能变成-1了?

所以,你如果同时有多个写请求的话,应该做好同步,就好像MSDN上面所说的用Synchronize
dengNeeo 2011-01-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 chaios 的回复:]

我了个囧,顶4楼
[/Quote] WO YE DING
vrhero 2011-01-11
  • 打赏
  • 举报
回复
无图无真相...

ps:五年不用hashtable了,都快忘了这是个啥东西了...
telankes2000 2011-01-11
  • 打赏
  • 举报
回复
那是因为楼主的结贴率过低所致
TimZhuFaith 2011-01-11
  • 打赏
  • 举报
回复
会么、、、
dengNeeo 2011-01-11
  • 打赏
  • 举报
回复
这怎么可能
胡扯

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧