我試圖從多線程添加到一個哈希集。如果該項目已存在,我想更新它,如果它不存在,我想將它添加到列表中。從多線程添加到HashSet時出錯
隨着我使用的代碼我結束了很多重複,我推測,因爲多個項目突然指向相同的參考。我不明白這是發生在哪裏或爲什麼。
下面是我使用的代碼,後面跟着「Log」字符串,當我第一次看到問題時結束。您可以看到突然添加的所有項目都具有相同的值。
lock (_remoteDevicesLock)
{
RemoteDevice rDevice = new RemoteDevice(notifyMessage.UUID, notifyMessage.Location);
log += notifyMessage.UUID + " " + rDevice.UUID;
if (!_remoteDevices.Add(rDevice))
{
log += " Not Added \r\n";
rDevice = (from d in _remoteDevices
where d.UUID.Trim().Equals(notifyMessage.UUID.Trim(), StringComparison.OrdinalIgnoreCase)
select d).FirstOrDefault();
if (rDevice != null)
{
//Update Device Expire Time
}
}
else
{
log += " Added \r\n Current HashSet: \r\n";
foreach (RemoteDevice rd in _remoteDevices)
{
log += rd.UUID + " \r\n";
}
}
}
00000000-0000-0001-1000-001cdf885737 00000000-0000-0001-1000-001cdf885737 Added
Current HashSet:
00000000-0000-0001-1000-001cdf885737
00000000-0000-0001-1000-001cdf885737 00000000-0000-0001-1000-001cdf885737 Not Added
00000000-0000-0001-1000-001cdf885737 00000000-0000-0001-1000-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Added
Current HashSet:
00000000-0000-0001-1000-001cdf885737
00000000-0000-0001-0002-001cdf885737
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0001-001cdf885737 00000000-0000-0001-0001-001cdf885737 Added
Current HashSet:
00000000-0000-0001-1000-001cdf885737
00000000-0000-0001-0002-001cdf885737
00000000-0000-0001-0001-001cdf885737
00000000-0000-0001-0001-001cdf885737 00000000-0000-0001-0001-001cdf885737 Not Added
00000000-0000-0001-0001-001cdf885737 00000000-0000-0001-0001-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Added
Current HashSet:
00000000-0000-0001-1000-001cdf885737
00000000-0000-0001-0002-001cdf885737
00000000-0000-0001-0001-001cdf885737
00000000-0000-0001-0000-001cdf885737
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-1000-001cdf885737 00000000-0000-0001-1000-001cdf885737 Not Added
00000000-0000-0001-1000-001cdf885737 00000000-0000-0001-1000-001cdf885737 Not Added
00000000-0000-0001-1000-001cdf885737 00000000-0000-0001-1000-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0001-001cdf885737 00000000-0000-0001-0001-001cdf885737 Not Added
00000000-0000-0001-0001-001cdf885737 00000000-0000-0001-0001-001cdf885737 Not Added
00000000-0000-0001-0001-001cdf885737 00000000-0000-0001-0001-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-1000-001cdf885737 00000000-0000-0001-1000-001cdf885737 Not Added
00000000-0000-0001-1000-001cdf885737 00000000-0000-0001-1000-001cdf885737 Not Added
00000000-0000-0001-1000-001cdf885737 00000000-0000-0001-1000-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0002-001cdf885737 00000000-0000-0001-0002-001cdf885737 Not Added
00000000-0000-0001-0001-001cdf885737 00000000-0000-0001-0001-001cdf885737 Not Added
00000000-0000-0001-0001-001cdf885737 00000000-0000-0001-0001-001cdf885737 Not Added
00000000-0000-0001-0001-001cdf885737 00000000-0000-0001-0001-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-0000-001cdf885737 00000000-0000-0001-0000-001cdf885737 Not Added
00000000-0000-0001-1000-001cdf885737 00000000-0000-0001-1000-001cdf885737 Added
Current HashSet:
00000000-0000-0001-0000-001cdf885737
00000000-0000-0001-0000-001cdf885737
00000000-0000-0001-0000-001cdf885737
00000000-0000-0001-0000-001cdf885737
00000000-0000-0001-1000-001cdf885737
更新:這裏是的GetHashCode和equals的要求,雖然我不認爲問題就出在這裏,因爲我是用列表,並附有手動檢查,也有問題。
public override bool Equals(object obj)
{
var other = obj as RemoteDevice;
if (other == null)
{
return false;
}
else
{
return UUID.Trim().Equals(other.UUID.Trim(), StringComparison.OrdinalIgnoreCase);
}
}
public override int GetHashCode()
{
return UUID.GetHashCode();
}
1.不要鎖定暴露在類之外的對象;您應該創建一個特定的對象來鎖定。2.不要使用字符串連接添加到日誌字符串,使用'StringBuilder'。 – Servy
RemoteDevice.Equals和RemoteDevice.GetHashCode是什麼樣子? – fsimonazzi
你可以檢查這個線程http://stackoverflow.com/questions/4306936/how-to-implement-concurrenthashset-in-net –