static void Main(string[] args)
{
Thread th1 = new Thread(fun1);
Thread th2 = new Thread(fun1);
th1.Name = "线程1"; //给线程命名,方便只有识别
th2.Name = "线程2---------";
th1.Start();
th2.Start();
Console.ReadKey();
}
public static void fun1()
{
if (Thread.CurrentThread.Name == "线程1")//可以通过字段 CurrentThread来获取当前线程,然后就可以判断是哪个线程在进行了
{
Thread.Sleep(20); //睡眠当前线程
}
for (int i = 0; i < 20; i++)
{
Console.WriteLine("这里是线程{0},当前执行{1}次", Thread.CurrentThread.Name, i);
}
}
从下列两图可以看出明显看出 睡眠的之是线程1
|
|