【发布时间】:2016-09-08 04:52:02
【问题描述】:
我有一个最近困扰我的线程问题。看看下面这个示例 C# 代码。
public void threading()
{
for(int count =0; count< 4; count++)
{
Thread childThread = new Thread(childThread);
childThread.start()
}
return;
}
public void childThread()
{
// Do alot of work
}
由于threading 方法中的循环之后有一个return 语句,threading 会在所有子线程完成执行之前执行return 语句吗?我在某处读到线程与分叉不同,因为它们不会创建单独的内存区域,那么死线程最终会在哪里?
【问题讨论】:
-
"
threading会在所有子线程完成执行之前执行 return 语句吗?" - 是的,因为它在单独的线程中运行。 -
“死线”是什么意思?
-
这是一劳永逸的代码。很少正确,您不知道线程何时完成并且很少希望正确处理错误。它让你问了这个问题。请考虑 Task 类。
标签: c# multithreading