C#多线程和线程池问题

时间:2024-03-22 20:05:14

C#多线程和线程池问题

static void Main(string[] args)
{
Thread threadA = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "A";
Thread threadB = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadB.Name = "B";
threadA.Priority = ThreadPriority.Highest;
threadB.Priority = ThreadPriority.BelowNormal;
threadB.Start();
threadA.Start();
Thread.CurrentThread.Name = "C";
ThreadMethod(new object());
Console.ReadKey();
}
public static void ThreadMethod(object parameter)
{
for (int i = ; i < ; i++)
{
Console.Write(Thread.CurrentThread.Name);
}
}
static void Main(string[] args)
{
Program pro = new Program();
Thread threadA = new Thread(pro.ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "王文建";
Thread threadB = new Thread(pro.ThreadMethod); //执行的必须是无返回值的方法
threadB.Name = "生旭鹏";
threadA.Start();
threadB.Start();
Console.ReadKey();
}
public void ThreadMethod(object parameter)
{
lock (this) //添加lock关键字
{
for (int i = ; i < ; i++)
{
Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
Thread.Sleep();
}
}
}
static void Main(string[] args)
{
Program pro1 = new Program();
Program pro2 = new Program();
Thread threadA = new Thread(pro1.ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "王文建";
Thread threadB = new Thread(pro2.ThreadMethod); //执行的必须是无返回值的方法
threadB.Name = "生旭鹏";
threadA.Start();
threadB.Start();
Console.ReadKey();
}
public void ThreadMethod(object parameter)
{
lock (this)
{
for (int i = ; i < ; i++)
{
Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
Thread.Sleep();
}
}
}

原文地址------------------------------------>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

多线程和线程池的一个全部解析——————————————————>>>>>>飞机票