namespace 线程的操作演示
{
class Program
{
static void Main(string[] args)
{
//1.通过启动一个新线程执行一个无参数,无返回值的方法
Thread t = new Thread(new ThreadStart(M1));
t.IsBackground = true;
t.Start();
//2.通过线程执行一个带一个参数的方法
Thread t = new Thread(new ParameterizedThreadStart(M2));
t.IsBackground = true;
t.Start("hello");
//3.启动线程,执行带多个参数的方法
Person p = new Person();
p.Name = "小李";
p.Age = 17;
p.Gender = "女";
Thread t = new Thread(new ThreadStart(p.Say));
t.IsBackground = true;
t.Start();
//4.获取线程编号
Console.WriteLine("主线程的编号:" + Thread.CurrentThread.ManagedThreadId);
//启动一个新线程
Thread t = new Thread(new ThreadStart(() =>
{
Console.WriteLine("这是一个新线程,线程编号是:" + Thread.CurrentThread.ManagedThreadId);
}));
t.IsBackground = true;
t.Start();
Console.WriteLine("ok");
Console.Read();
}
static void M2(object obj)
{
Console.WriteLine(obj);
}
static void M1()
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine(". ");
}
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public void Say()
{
Console.WriteLine(this.Name + " " + this.Age + " " + this.Gender);
}
}
}
线程的基本操作
最新推荐文章于 2023-05-09 17:40:24 发布