using System;
using System.Threading;
public class MyThread
{
public double Diameter = 10;
public double Result = 0;
public MyThread(int Diameter)
{
this.Diameter = Diameter;
}
public void Calculate()
{
Result = Diameter * Math.PI;
Console.WriteLine("Calculate End, Diameter is {0},Result is {1}", this.Diameter, Result);
}
}
public class Test
{
public static void Main(String[] args)
{
Thread.CurrentThread.Name = "主线程";
Console.WriteLine(string.Format("当前线程:{0} 开始", Thread.CurrentThread.Name));
Thread[] threadArr = new Thread[30];
for (int i = 1; i <= threadArr.Length; i++)
{
MyThread t = new MyThread(i);
ThreadStart threadStart = new ThreadStart(t.Calculate);
threadArr[i - 1] = new Thread(threadStart);
threadArr[i - 1].Name = i.ToString();
threadArr[i - 1].Start();
//threadArr[i-1].Join(); //写在此处会让30个线程顺序执行而失去意义
}
foreach (Thread th in threadArr)
th.Join();
//中止线程,必不可少。
foreach (Thread th in threadArr)
th.Abort();
Console.WriteLine(string.Format("当前线程:{0} 结束", Thread.CurrentThread.Name));
Console.Read();
}
}