我有这段代码:
noObjs = 0
Dim oName As String
Dim i As Integer
Dim tripleIndex As Integer = 0
Do While sr.Peek() <> -1
readCSV = sr.ReadLine.Split(sepChar(0))
If readCSV.Length >= 3 Then
oName = readCSV(0)
For i = noObjs - 1 To 0 Step -1
If oName = objNames(i) Then
obIndOfTriple(tripleIndex) = i
Exit For
End If
Next i
If i = -1 Then
objNames(noObjs) = oName
obIndOfTriple(tripleIndex) = noObjs
noObjs += 1
End If
End If
tripleIndex += 1
Loop
sr.Close()
我正在尝试这样并行化:
noObjs = 0
Dim oName As String
Dim i As Integer
Dim tripleIndex As Integer = 0
Dim allData() As String = File.ReadAllLines(in_file)
Parallel.For(0, allData.Count, Sub(k)
readCSV = allData(k).Split(sepChar(0))
If readCSV.Length >= 3 Then
oName = readCSV(0)
For i = noObjs - 1 To 0 Step -1
If oName = objNames(i) Then
obIndOfTriple(tripleIndex) = i
Exit For
End If
Next i
If i = -1 Then
objNames(noObjs) = oName
obIndOfTriple(tripleIndex) = noObjs
noObjs += 1
End If
End If
tripleIndex += 1
End Sub)
但是,我在以下位置得到一个“索引超出了数组的范围”:
If oName = objNames(i) Then
我还应该在这里提到 objNames() 和 obIndOfTriple() 是全局声明的(具有固定大小)。通过一些搜索,我了解到这与线程安全有关,尽管我仍然是并行性的新手。谁能指出我正确的方向?谢谢。