环境:
线程的状态可以从枚举ThreadState
中查看到:
public enum ThreadState
{
Running = 0x0,
StopRequested = 0x1,
SuspendRequested = 0x2,
Background = 0x4,
Unstarted = 0x8,
Stopped = 0x10,
WaitSleepJoin = 0x20,
Suspended = 0x40,
AbortRequested = 0x80,
Aborted = 0x100
}
我们先抛开Background
不谈,说一下其他的九种状态:
注意:
不要使用Suspend和Resume方法来同步线程的活动。当你Suspend线程时,您无法知道线程正在执行什么代码。如果在安全权限评估期间线程持有锁时挂起线程,则AppDomain中的其他线程可能会被阻塞。如果线程在执行类构造函数时Suspend,则试图使用该类的AppDomain中的其他线程将被阻塞。死锁很容易发生。
using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Threading;
namespace TestDI
{
class Program
{
public static void Main(string[] args)
{
Test6();
Console.WriteLine("ok");
Console.ReadLine();
}
private static void Test6()
{
var threadMain = Thread.CurrentThread;
var thread = new Thread(() =>
{
Thread.Sleep(2000);
for (int i = 0; i < 10000; i++)
{
File.AppendAllText("d:\\temp.txt", i + "\r\n");
}
//由于这个线程被主线程执行thread.Join(),从而导致主线程被阻塞,所以主线程状态为:WaitSleepJoin
Console.WriteLine("主线程状态:" + threadMain.ThreadState);
});
//线程thread刚被创建,还未启动,状态为:Unstarted
Console.WriteLine(thread.ThreadState);
thread.Start();
//线程thread启动后,线程的状态变为:Running
Console.WriteLine(thread.ThreadState);
Thread.Sleep(1000);
//线程thread此时执行到Thread.Sleep(2000);,状态为:WaitSleepJoin
Console.WriteLine(thread.ThreadState);
thread.Join();
//线程被执行了Join导致此处代码被阻塞,这里获取的状态肯定是Stopped
Console.WriteLine(thread.ThreadState);
//主线程状态也从WaitSleepJoin改为Running
Console.WriteLine("主线程:" + threadMain.ThreadState);
}
}
}
.netcore不支持Abort,而且线程的正确终止方法应该是通过信号灯让线程本身结束,而不是强制结束,因为你无法预测线程正在做什么!
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Test5();
Console.WriteLine("ok");
Console.ReadLine();
}
private static void Test5()
{
var thread = new Thread(() =>
{
try
{
Thread.Sleep(2000);
}
catch (Exception ex)
{
Console.WriteLine("异常:" + ex.Message);
}
for (int i = 0; i < 10000; i++)
{
File.AppendAllText("d:\\temp.txt", i + "\r\n");
}
});
//线程thread刚被创建,还未启动,状态为:Unstarted
Console.WriteLine(thread.ThreadState);
thread.Start();
//线程thread启动后,线程的状态变为:Running
Console.WriteLine(thread.ThreadState);
Thread.Sleep(1000);
//线程thread此时执行了Thread.Sleep(2000);,状态为:WaitSleepJoin
Console.WriteLine(thread.ThreadState);
thread.Abort();
Thread.Sleep(500);
//线程thread已经Aborted了
Console.WriteLine(thread.ThreadState);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Test4();
Console.WriteLine("ok");
Console.ReadLine();
}
private static void Test4()
{
var thread = new Thread(() =>
{
try
{
Thread.Sleep(2000);
}
catch (Exception ex)
{
Console.WriteLine("异常:" + ex.Message);
}
for (int i = 0; i < 1000; i++)
{
File.AppendAllText("d:\\temp.txt", i + "\r\n");
}
});
//线程thread刚被创建,还未启动,状态为:Unstarted
Console.WriteLine(thread.ThreadState);
thread.Start();
//线程thread启动后,线程的状态变为:Running
Console.WriteLine(thread.ThreadState);
Thread.Sleep(1000);
//线程thread此时执行了Thread.Sleep(2000);,状态为:WaitSleepJoin
Console.WriteLine(thread.ThreadState);
thread.Suspend();
Thread.Sleep(500);
//线程thread被执行Suspend(),状态为SuspendRequested, WaitSleepJoin,并引发异常
Console.WriteLine(thread.ThreadState);
if (thread.ThreadState == (ThreadState.SuspendRequested | ThreadState.WaitSleepJoin))
{
Console.WriteLine("ThreadState.SuspendRequested | ThreadState.WaitSleepJoin\t" + true);
}
Thread.Sleep(1500);
//线程thread此时已从Sleep中苏醒,状态为Suspend
Console.WriteLine(thread.ThreadState);
thread.Resume();
Thread.Sleep(500);
//线程thread被执行Resume(),状态为Running
Console.WriteLine(thread.ThreadState);
Thread.Sleep(10000);
//线程thread已经执行完毕,状态为Stopped
Console.WriteLine(thread.ThreadState);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Test3();
Console.WriteLine("ok");
Console.ReadLine();
}
///
/// 测试线程状态Unstarted->Running->WaitSleepJoin->SuspendRequested, WaitSleepJoin->WaitSleepJoin->Running->Stopped
/// 线程先睡眠再挂起,然后解除挂起,然后自动唤醒
/// .netcore不支持Suspend/Resume,而且这两个方法已经标记为废弃的api
///
private static void Test3()
{
var thread = new Thread(() =>
{
try
{
Thread.Sleep(2000);
}
catch (Exception ex)
{
Console.WriteLine("异常:" + ex.Message);
}
for (int i = 0; i < 1000; i++)
{
File.AppendAllText("d:\\temp.txt", i + "\r\n");
}
});
//线程thread刚被创建,还未启动,状态为:Unstarted
Console.WriteLine(thread.ThreadState);
thread.Start();
//线程thread启动后,线程的状态变为:Running
Console.WriteLine(thread.ThreadState);
Thread.Sleep(1000);
//线程thread此时执行了Thread.Sleep(2000);,状态为:WaitSleepJoin
Console.WriteLine(thread.ThreadState);
thread.Suspend();
Thread.Sleep(500);
//线程thread被执行Suspend(),状态为SuspendRequested, WaitSleepJoin
Console.WriteLine(thread.ThreadState);
thread.Resume();
Thread.Sleep(500);
//线程thread被执行Resume(),状态为WaitSleepJoin
Console.WriteLine(thread.ThreadState);
Thread.Sleep(2000);
//线程thread已经从Sleep中苏醒,状态为Running
Console.WriteLine(thread.ThreadState);
Thread.Sleep(8000);
//线程thread已经执行完毕,状态为Stopped
Console.WriteLine(thread.ThreadState);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Test2();
Console.WriteLine("ok");
Console.ReadLine();
}
///
/// 测试线程状态Unstarted->Running->WaitSleepJoin->Running->Stopped
/// 休眠后使用Interrupt方法唤醒
///
private static void Test2()
{
var thread = new Thread(() =>
{
try
{
Thread.Sleep(2000);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
for (int i = 0; i < 1000; i++)
{
File.AppendAllText("d:\\temp.txt", i + "\r\n");
}
});
//线程thread刚被创建,还未启动,状态为:Unstarted
Console.WriteLine(thread.ThreadState);
thread.Start();
//线程thread启动后,线程的状态变为:Running
Console.WriteLine(thread.ThreadState);
Thread.Sleep(1000);
//线程thread此时执行了Thread.Sleep(2000);,状态为:WaitSleepJoin
Console.WriteLine(thread.ThreadState);
thread.Interrupt();
Thread.Sleep(100);
//线程thread被执行Interrupt(),状态为Running
Console.WriteLine(thread.ThreadState);
Thread.Sleep(10 * 1000);
//线程thread已经执行完毕,状态为Stopped
Console.WriteLine(thread.ThreadState);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Test1();
Console.WriteLine("ok");
Console.ReadLine();
}
///
/// 测试线程状态Unstarted->Running->WaitSleepJoin->Running->Stopped
/// 休眠到期后自动唤醒
///
private static void Test1()
{
var thread = new Thread(() =>
{
Thread.Sleep(2000);
for (int i = 0; i < 1000; i++)
{
File.AppendAllText("d:\\temp.txt", i + "\r\n");
}
});
//线程thread刚被创建,还未启动,状态为:Unstarted
Console.WriteLine(thread.ThreadState);
thread.Start();
//线程thread启动后,线程的状态变为:Running
Console.WriteLine(thread.ThreadState);
Thread.Sleep(1000);
//线程thread此时执行了Thread.Sleep(2000);,状态为:WaitSleepJoin
Console.WriteLine(thread.ThreadState);
Thread.Sleep(1500);
//线程thread此时正在运行,状态为Running
Console.WriteLine(thread.ThreadState);
Thread.Sleep(8000);
//线程thread此时已经运行完毕,状态为Stopped
Console.WriteLine(thread.ThreadState);
}
}
}