나는이 작은 기능을 발견했지만 제대로 호출하려고 노력하고 있습니다. 어떻게 내 ConcurrentDictionary를 업데이트하기 위해 호출 할 수 있습니다차원 동시 사전 업데이트
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Animals As New Concurrent.ConcurrentDictionary(Of String, String())
Dim iKey = "key123456"
Animals(iKey) = {"cat", "dog", "bird"}
Dim success As Boolean = TryUpdate(Animals, iKey, Func("cat", "frog"))
End Sub
Function TryUpdate(Of TKey, TValue)(dict As Concurrent.ConcurrentDictionary(Of TKey, TValue), key As TKey, updateFactory As Func(Of TValue, TValue)) As Boolean
Dim curValue As TValue
If Not dict.TryGetValue(key, curValue) Then
Return False
End If
dict.TryUpdate(key, updateFactory(curValue), curValue)
Return True
End Function
End Class
TryUpdate 대신 'AddOrUpdate'를 사용할 수 있습니다. TryGetValue를 실행 한 다음 TryUpdate를 실행하면 동시성 문제가 발생할 수 있습니다. 즉, 검사를 수행 할 때 값이 존재하지 않을 수 있지만 다른 스레드에서 TryUpdate를 수행하면 값이 사라질 수 있습니다. – Neolisk