gpt4 book ai didi

c# - thread.join 是否同时启动所有线程

转载 作者:行者123 更新时间:2023-11-30 21:55:00 51 4
gpt4 key购买 nike

    Thread[] threads = new Thread[12];
int temp;

//_stopRequest = false;
for (int i = 0; i < threads.Length - 1; i++)
{
temp = i;
threads[temp] = new Thread(new ThreadStart(() => test(test1[temp],"start", temp)));
threads[temp].Start();
//threads[temp].Join();
}

for(int i=0; i<threads.Length-1; i++)
{
threads[i].Join();
}

谁能解释一下

最佳答案

不,线程是在您调用 Start() 时启动的。

如果您在 Start() 之后立即调用 Join()(注释掉的代码),每个线程都会启动,然后当前线程的执行将停止,直到第一个线程停止。所以它实际上会充当单个线程。

代码现在的方式是,所有线程都启动,然后当前线程等待所有启动的线程完成。

关于c# - thread.join 是否同时启动所有线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32673670/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com