다음 루프를 병렬 루프로 변환하는 데 도움을주십시오. 나는 Parallel.ForEach와 ConcurrentBag를 HashSet 대신에 사용하려했지만, 매치 된 것은 매번 다른 결과를 반환한다는 것이다.Parallel.ForEach는 매번 다른 결과를 나타냅니다
알아낼 수 없습니다 ... 스레드 안전 문제 때문입니까?
키워드 목록에는 길이가 각각 1-3 단어 인 약 500 개의 고유 문자열이 있습니다.
항목에는 약 10000 개의 항목이 포함되어 있습니다.
원래 코드 :
Dim Items As IEnumerable(Of Item) = Db.Items.GetAll
Dim Keywords As HashSet(Of String)
Dim Matched As Concurrent.ConcurrentBag(Of Item)
Threading.Tasks.Parallel.ForEach(Of Item)(Items, Sub(Item)
For Each Keyword In Keywords
If Regex.IsMatch(Item.Title, String.Format("\b{0}\b", Keyword), RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant) Then
If Not Matched.Contains(Item) Then
Matched.Add(Item)
End If
Continue For
End If
Next
End If
이 두 샘플을 다른 방법으로 발견했습니다. 변경하는 데 지쳐 있었지만 이러한 샘플과 같은 통합 대신 컬렉션 및 개체에서 작동하지는 못했습니다. 스빅, 제 생각에는 이것들이 효과가 있다고 생각합니까? http://technet.microsoft.com/subscriptions/dd460699.aspx http://tipsandtricks.runicsoft.com/CSharp/ParallelClass.html – jjarv