简单的线程Thread使用

static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
aa a = new aa();
a.age = i;
Thread thread = new Thread(new ThreadStart(a.getNum)); //参数和方法写到一个类里面也可以传参
thread.Start();

Thread.Sleep(2000);
}
}
public static void getNum(object i)
{

Console.WriteLine(((aa)i).age.ToString());
Console.ReadKey();
}

public class aa {
public string name { get; set; }
public int age { get; set; }
public void getNum()
{
Console.WriteLine(age.ToString());
Console.ReadKey();
}
}

2.带参数的

static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
aa a = new aa();
a.age = i;
Thread thread = new Thread(new ParameterizedThreadStart(getNum)); //这么写可以在getNum里设断点
thread.Start(a);//参数

Thread.Sleep(2000);
}
}

上一篇:Tomcat9报错 The valid characters are defined in RFC 7230 and RFC 3986


下一篇:Window.navigator