class Program
{
private static int sum = 0;
private static int SUM()
{
for (int i = 0; i <= 100; i++)
{
sum += i;
}
return sum;
}
static void Foo()
{
for (int i = 0; i < 100; i++)
{
int rst = 0;
Thread t = new Thread(() =>
{
rst = SUM();
});
t.Start();
//join方法主要是用来阻塞调用线程
t.Join();
sum = 0;
Console.WriteLine(" 索引{0} 结果{1}", i, rst);
}
}
static void Main(string[] args)
{
Foo();
Console.Read();
}
}
调用join输出的结果
注释掉join时输出的结果