Hashtable 问题 急

zy422 2007-07-01 09:41:25
当我从线程A里把 Key Val Add到 Hashtable tab 中后,在线程B却得不到值?
tab 为 static 静态的,

测试从 线程A里能取到值val

程序调试 在线程 B中 可以看到 tab里的val值和key

...全文
261 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
GXY2005 2007-07-02
  • 打赏
  • 举报
回复
呵呵!
cnming 2007-07-02
  • 打赏
  • 举报
回复
写个最简单的先
static HashTable ht = new HashTable();
Thread th1,th2;

Form_Load(object sender, EventArgs e)
{
th1 = new Thread(new ThreadStart(this.InsertHash));
th1.Start();
th2 = new Thread(new ThreadStart(this.GetHash));
th2.Start();
}
private void InsertHash()
{
lock (ht)
{
for (int i = 0; i < 1000000; i++)
{
ht.Add(i, "string"+i);
}


}
}

private void GetHash()
{
lock (ht)
{
string s = ht[999999].ToString();
MessageBox.Show(s);
}
}



很明显,你在调用string s = ht[999999].ToString();的时候,ht[999999]没有建立的概率是90%以上

zy422 2007-07-01
  • 打赏
  • 举报
回复
找到问题了,原来是 GetMtserver(String ^servername )采用的编码不对

调试器里发现不了。
jiatong1981 2007-07-01
  • 打赏
  • 举报
回复
Hashtable^ synctab=Hashtable::Synchronized(connectiontable);
//这里错误
不知道你为何不直接操作当前的hashtable
jiatong1981 2007-07-01
  • 打赏
  • 举报
回复
当然这样用效率并不高
所以还需用Moniter类来完成
zy422 2007-07-01
  • 打赏
  • 举报
回复
很抱歉,我不是用C# 写的,是用 VC.NET 写的

static Hashtable ^connectiontable=gcnew Hashtable;

Server^ ServerCollection::GetMtserver(String ^servername )
{
pmut->WaitOne();
Server^ server=nullptr;
String ^key=gcnew String(servername);

Monitor::Enter(connectiontable);
server=(Server^)(connectiontable[key]);
Monitor::Exit(connectiontable);

pmut->ReleaseMutex();
return server;
}
void ServerCollection::AddMtserver(String ^servername ,Server ^value)
{
pmut->WaitOne();
String ^key=gcnew String(servername);
Hashtable^ synctab=Hashtable::Synchronized(connectiontable);
Monitor::Enter(connectiontable);
synctab->Add(key,value);
Monitor::Exit(connectiontable);
pmut->ReleaseMutex();
}
jiatong1981 2007-07-01
  • 打赏
  • 举报
回复
写个最简单的先
static HashTable ht = new HashTable();
Thread th1,th2;

Form_Load(object sender, EventArgs e)
{
th1 = new Thread(new ThreadStart(this.InsertHash));
th1.Start();
th2 = new Thread(new ThreadStart(this.GetHash));
th2.Start();
}
private void InsertHash()
{
lock (ht)
{
for (int i = 0; i < 1000000; i++)
{
ht.Add(i, "string"+i);
}


}
}

private void GetHash()
{
lock (ht)
{
string s = ht[999999].ToString();
MessageBox.Show(s);
}
}
jiatong1981 2007-07-01
  • 打赏
  • 举报
回复
代码贴出来看看
zy422 2007-07-01
  • 打赏
  • 举报
回复
已经试了 Monitor.Enter Monitor.Exit 还是不对
vicqqq 2007-07-01
  • 打赏
  • 举报
回复
up
blackant2 2007-07-01
  • 打赏
  • 举报
回复
支持cnming(cnming)
jiatong1981 2007-07-01
  • 打赏
  • 举报
回复
初步估计是你在操作时没有考虑多线程安全性

hashtable的有关多线程安全性 参看以下代码

Item item = null;
bool lockWasSuccessful = false;

while(true) {
lock(hashtable) {
item = hashtable[key];
lockWasSuccessful = Monitor.TryEntry(item);
}
if(lockWasSuccessful == false) {
Thread.Sleep(0);
continue;
}
// If we reach here, the item was successfully locked
try {
// Application code goes here
}
finally {
Monitor.Exit(item);
}
break;
}
cnming 2007-07-01
  • 打赏
  • 举报
回复
问题出在你的A、B线程不同步

可以使用Monitor来保证数据安全

http://blog.csdn.net/cnming/archive/2007/06/30/1672297.aspx
jiatong1981 2007-07-01
  • 打赏
  • 举报
回复
代码?

7,776

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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