C# 多线程——中
4、多线程实现
(1)ThreadStart委托和ParameterizedThreadStart委托
在调用Thread.Start()方法后,系统以异步的方式运行threadstart或ParameterizedThreadStart委托绑定的方法。ParameterizedThreadStart委托是面向带参数方法的,对应方法的参数为object,此参数可以是一个值对象或自定义对象。
class Program
{
static Thread thread_1;
static Thread thread_2;
static void Main(string[] args)
{
Console.WriteLine("Start Run...");
//使用threadstart启动线程(无参数)
ThreadStart ts_1 = new ThreadStart(count_1);
thread_1 = new Thread(ts_1);
thread_1.Start();
thread_1.Join();//阻塞线程,调用线程结束后执行其他线程
Console.WriteLine("other thread");
//使用ParameterizedThreadStart启动线程(有参数)
ParameterizedThreadStart pts_2 = new ParameterizedThreadStart(count_1);
thread_2 = new Thread(pts_2);
thread_2.Start(100);
thread_2.Join();
Console.WriteLine("Run Over...");
}
static void count_1()
{
for(int i=0;i<5;i++)
{
Console.WriteLine(
637




被折叠的 条评论
为什么被折叠?



