using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace ConsoleApplication1{ ////// 声明一个委托 /// /// ///delegate double CalculateMethod(double Diameter); class Program { /// /// 定义委托对象 /// public static CalculateMethod calcMethod; ////// 计算结果 /// public static double result = 0; static void Main(string[] args) { /***********一、简单线程***********/ //ThreadStart threadStart = new ThreadStart(Calculate); //Thread thread = new Thread(threadStart); //thread.Start(); /***********二、线程传递单个参数******************/ //使用这个这个委托定义的线程的启动函数可以接受一个输入参数 //ParameterizedThreadStart threadStart = new ParameterizedThreadStart(Calculate); //Thread thread = new Thread(threadStart); //thread.Start(0.9); /***********三、线程使用线程类调用方法******************/ //MyThread t = new MyThread(5.0); //ThreadStart threadStart = new ThreadStart(t.Calculate); //Thread thread = new Thread(threadStart); //thread.Start(); /***********四、把参数传递变成了属性共享,把逻辑和逻辑涉及的数据封装在一起(匿名方法)******************/ //double Diameter = 0.9d; //Thread thread = new Thread(new ThreadStart(delegate() //{ // Console.WriteLine("Calculate Start"); // Thread.Sleep(2000); // Console.WriteLine("匿名方法: The perimeter Of Circle with a Diameter of {0} is {1}", Diameter, Diameter * Math.PI); ; // Console.Read(); //})); //thread.Start(); /***********王、线程池******************/ WaitCallback w = new WaitCallback(Calculate); ThreadPool.QueueUserWorkItem(w, 1.0); ThreadPool.QueueUserWorkItem(w, 2.0); ThreadPool.QueueUserWorkItem(w, 3.0); ThreadPool.QueueUserWorkItem(w, 4.0); } ////// 简单线程调用方法 /// /// public static void Calculate() { double Diameter = 0.9d; Console.WriteLine("Calculate Start"); Thread.Sleep(2000);//睡2秒 Console.Write("简单线程:The perimeter Of Circle with a Diameter of {0} is {1}", Diameter, Diameter * Math.PI); Console.Read(); } ////// 传递单个参数线程启用方法 /// /// public static void Calculate(object arg) { double Diameter = (double)arg; Console.WriteLine("Calculate Start"); Thread.Sleep(2000);//睡2秒 Console.Write("线程传递单个参数:The perimeter Of Circle with a Diameter of {0} is {1}", Diameter, Diameter * Math.PI); Console.Read(); } ////// 线程池调用方法 /// /// ///public static void Calculate(double Diameter) { Console.WriteLine("Calculate Start"); Thread.Sleep(2000);//睡2秒 Console.Write("线程传递单个参数:The perimeter Of Circle with a Diameter of {0} is {1}", Diameter, Diameter * Math.PI); Console.Read(); } } /// /// 线程类(将线程要执行的方法和它需要的参数封装到类) /// public class MyThread { public double Diameter = 10; public double Result = 0; public MyThread(double Diameter) { this.Diameter = Diameter; } public void Calculate() { Console.WriteLine("Calculate Start"); Thread.Sleep(2000);//睡2秒 Console.WriteLine("Calculate End, Diameter is {0},Result is {1}", this.Diameter, Diameter * Math.PI); Console.Read(); } }}