我有 2863 个对象的数组。我希望每 1000 个对象有两次“运行”,通过 4 个线程(运行 PC # of CPU)读取数组数据。
目前我的源代码正在对数据进行分区以正确的线程数并运行:
单次运行大小(默认)= 1000 个元素
运行数 = 2
额外线程运行大小 = 866 个元素
开始运行 [1 / 2]
线程作为 readDCMTags(i=0,firstIndex=0, lastIndex=249
线程作为 readDCMTags(i=1 ,firstIndex=250, lastIndex=499
线程作为 readDCMTags(i=2,firstIndex=500, lastIndex=749
线程作为 readDCMTags(i=3,firstIndex=750, lastIndex=999
开始运行 [2 / 2]
线程作为 readDCMTags(i =0,firstIndex=1000, lastIndex=1249
线程作为 readDCMTags(i=1,firstIndex=1250, lastIndex=1499
线程作为 readDCMTags(i=2,firstIndex=1500, lastIndex=1749
线程作为 readDCMTags(i=3,firstIndex= 1750, lastIndex=1999
额外线程作为 readDCMTags(i=1,firstIndex=2000, lastIndex=2865
然而,当前源代码一次启动所有线程,而不是等待 RUN TO END。当我从当前运行中加入线程时,GUI 正在挂起。如何解决问题?
源代码是:
nrOfChunks = 2866 / 1000;
int leftOverChunk = 2866 % 1000;
for(int z = 0; z < nrOfChunks; z++)
{
addToStatusPanel("\nStarting run [" + (z+1).ToString() + " / " + nrOfChunks.ToString() + "]");
int indexesPerThread = 1000 / 5; #nrOfThreads
int leftOverIndexes = 1000 % 5; #nrOfThreads
threads = new Thread[nrOfThreads];
threadProgress = new int[nrOfThreads];
threadDCMRead = new int[nrOfThreads];
for(int i = 0; i < nrOfThreads; i++)
{
int firstIndex = (i * indexesPerThread+z*Convert.ToInt32(chunkSizeTextBox.Text));
int lastIndex = firstIndex + indexesPerThread - 1;
if(i == (nrOfThreads- 1))
{
if(i == (nrOfThreads - 1))
{
lastIndex += leftOverIndexes;
}
}
addToStatusPanel("readDCMTags(i=" + i.ToString() + ",firstIndex=" + firstIndex.ToString() + ", lastIndex=" + lastIndex.ToString());
threads[i] = new Thread(() => readDCMTags(i.ToString(), firstIndex, lastIndex));
threads[i].Name = i.ToString();
threads[i].Start();
}
if(z == (nrOfChunks - 1))
{
int firstIndex = (nrOfChunks * Convert.ToInt32(chunkSizeTextBox.Text));
int lastIndex = firstIndex + leftOverChunk - 1;
addToStatusPanel("readDCMTags(i=" + z.ToString() + ",firstIndex=" + firstIndex.ToString() + ", lastIndex=" + lastIndex.ToString());
}
}
在循环之后添加for(int i = 0; i < nrOfThreads; i++)
线程数组的连接命令,在进行下一个运行循环之前for(int z = 0; z < nrOfChunks; z++)
挂起 GUI。