【发布时间】:2025-11-22 22:20:03
【问题描述】:
所以这是我一直想知道的事情..
假设我想要 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);
}
谢谢大家!
【问题讨论】:
-
最终目标是什么?就为了有 10 个线程?
-
这就是数组和 List
存在的原因。
标签: c# variables variable-assignment