我正在探索在另一个线程中启动线程的概念。这是我提出的代码,这是我正在开发的另一个程序的淡化版本但是我发现第二级线程没有成功完成。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication4
{
public class SomeClassA
{
public SomeClassA(string display)
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine(display);
}
}
public class MainSomeClassA
{
public List<SomeClassA> SomeClassaAList;
public List<Thread> ThreadList;
public MainSomeClassA()
{
ThreadList = new List<Thread>();
SomeClassaAList = new List<SomeClassA>();
for (int i = 0; i < 10; i++)
{
ThreadList.Add(new Thread(() => StartThread("Hello")));
}
WaitComplete();
}
public void WaitComplete()
{
bool AllThreadsAlive = true;
while (AllThreadsAlive)
{
AllThreadsAlive = false;
foreach (Thread t in ThreadList)
{
if (t.IsAlive)
{
AllThreadsAlive = true;
}
}
}
}
public void StartThread(string display)
{
SomeClassaAList.Add(new SomeClassA(display));
}
}
class Program
{
public static List<MainSomeClassA> MainSomeClassAList = new List<MainSomeClassA>();
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
MainSomeClassAList = new List<MainSomeClassA>();
List<Thread> ThreadList = new List<Thread>();
bool threadsAlive = true;
sw.Reset();
sw.Start();
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(AddToMainClassAList);
t.Start();
ThreadList.Add(t);
}
while (threadsAlive)
{
threadsAlive = false;
foreach (Thread t in ThreadList)
{
if (t.IsAlive)
{
threadsAlive = true;
}
}
}
sw.Stop();
Console.WriteLine("Elapsed Time: {0}", sw.ElapsedMilliseconds);
Console.ReadKey();
}
public static void AddToMainClassAList()
{
MainSomeClassAList.Add(new MainSomeClassA());
}
}
}
上面的代码没有打印出来&#34;你好&#34;并退出而不创建SomeClassA
列表。
答案 0 :(得分:1)
您的代码的问题在于您永远不会启动内部线程。将构造函数更改为如下所示,它将起作用:
public MainSomeClassA()
{
ThreadList = new List<Thread>();
SomeClassaAList = new List<SomeClassA>();
for (int i = 0; i < 10; i++)
{
ThreadList.Add(new Thread(() => StartThread("Hello")));
// Start thread here:
ThreadList[ThreadList.Count - 1].Start();
}
WaitComplete();
}
那就是说,我应该指出,你很幸运,程序并没有崩溃。您有十个线程同时尝试修改MainSomeClassAList
对象,其中一些必然会强制重新分配内部缓冲区。实际上,如果您在结尾打印出列表的Count
,您会发现它不应该是10,因为它应该是。
要使代码真正正确,您需要在Add()
方法的AddToMainClassAList()
调用周围添加同步。同样的事情适用于StartThread()
方法和SomeClassaAList
对象。
最后,等待线程的方法很差。你应该尽量避免轮询。在这种情况下,Thread.Join()
方法是一个合理的选择(您应该尽量避免阻塞线程,但对于此示例,它是不可避免的)。例如,您的繁忙循环可以替换为:
foreach (Thread thread in ThreadList)
{
thread.Join();
}