所以这是我一直想知道的事情..
假设我想要 10 个线程,称为 t1 t2 t3 等……但我不想写
Thread t1 = new Thread(run);
Thread t2 = new Thread(run);
Thread t3 = new Thread(run);
...
而是这样的:(伪代码)
for(int i = 0; i <= 10; i++){
Thread t + i = new Thread(run);
}
有没有办法做到这一点?
提前致谢。
编辑:好的,所以这基本上是我想做的:
static void Main(string[] args)
{
int n = 0;
Program p = new Program();
List<Thread> threads = new List<Thread>();
for(int i = 0; i <= 10; i++)
{
threads.Add(new Thread(p.run));
}
foreach(Thread t in threads)
{
n++;
t.Name = "Thread " + n;
t.IsBackground = true;
t.Start();
}
Console.ReadKey(false);
}
谢谢大家!