我正在使用 tpl 并行执行数据。问题是有时它会无缘无故挂起,给出这样的输出:
The thread '<No Name>' (0x4aa4) has exited with code 0 (0x0).
The thread '<No Name>' (0x2bf4) has exited with code 0 (0x0).
The thread '<No Name>' (0x417c) has exited with code 0 (0x0).
The thread '<No Name>' (0x432c) has exited with code 0 (0x0).
The thread '<No Name>' (0x3ad0) has exited with code 0 (0x0).
The thread '<No Name>' (0x4440) has exited with code 0 (0x0).
The thread '<No Name>' (0x24e8) has exited with code 0 (0x0).
The thread '<No Name>' (0x3354) has exited with code 0 (0x0).
The thread '<No Name>' (0x4a30) has exited with code 0 (0x0).
该程序仍将执行,当我在 Visual Studio 中暂停它时,它会在我有 ForEach 的并行过程的地方暂停:
Parallel.ForEach
(
hold1.remainingHolds.ToArray(),
subSequence =>
{
//only if subsequence has 1 common substring
if (checkIfCommon(remainingSequence, subSequence.ToString()) == 1)
{
goDownLinkType2(subSequence.ToString().Split(','), 0,
(SuffixNode)hold1.childHolds[subSequence.ToString().Split(',')[0]]);
}
}
);
我不认为它是死锁,因为没有任何情况下可以有一个线程等待不同的资源并最终相互锁定
它应该进入这个方法内部并递归下去一个后缀树,直到它会等到没有线程将任何对象添加到 arraylist
Boolean flag = false;
public void goDownLinkType2
(string[] splittedString,
int index, SuffixNode currentNode)
{
Boolean writingFlag = false;
if (index == 2)
{
while(writingFlag == false)
{
while(flag == true)
{
//blocked
}
if (flag == false)
{
flag = true;
if(!secondChoiceResults.Contains
(currentNode.representingStance.SequenceOfHolds))
{
Console.WriteLine("new addition");
secondChoiceResults.Add
(currentNode.representingStance.SequenceOfHolds,
currentNode.representingStance);
}
flag = false;
writingFlag = true;
}
}
}
else
{
int nextIndex = index + 1;
goDownLinkType2
(splittedString,
nextIndex,
(SuffixNode)currentNode.childHolds[splittedString[nextIndex]]
);
}
}