文章目录
- 一、线程(Thread)
-
- 1.1 什么是线程Thread
- 1.2 线程的一些属性
- 1.3 Thread.Join() & Thread.Sleep()
- 1.4 阻塞 blocking
- 1.4.1 解除阻塞
- 1.4.2 上下文切换
- I/O-bound vs Compute-bound(或Cpu-Bound)
- 1.5 线程安全
- 1.6 向线程传递数据
- 1.7 异常处理
- 1.8 前台和后台线程
- 1.9 线程优先级
- 1.10 信号
- 1.11 线程池(Thread Pool)
- 1.12 Task
- 1.13 Task的返回值
- 1.14 Task的异常
- 1.15 Coninuation(继续、延续)
- 1.16 TaskCompletionSource
- 1.17 同步异步
- 1.18 异步函数
- 1.19 编写异步函数
- 1.20 异步中的同步上下文
- 1.21 优化同步完成
- 1.22 TAP
一、线程(Thread)
1.1 什么是线程Thread
- 线程是一个可执行路径,它可以独立于其他线程执行。
- 每个线程都在操作系统的进程(Process)内执行,而操作系统进程提供了程序运行的独立环境。
- 单线程应用:一个进程中只有一个线程
- 多线程应用:一个进程中有多个线程,多个线程会共享当前的执行环境(如内存)
//开辟了一个的新的线程Thread
Thread t = new Thread(WryteY);
t.Start();
//主线程在做的一些事
for(int i=0; i<1000; i++)
{
Console.Write("x");
}
void WryteY(object obj)
{
for(int i=0; i < 1000; i++)
{
Console.Write("y");
}
}
上例讲解:
在单核计算机上,操作系统必须为每个线程分配"时间片"来模拟并发,从而导致重复的x和y。
在多核或多处理器计算上,这两个线程可以真正地并行执行(也可能受到计算机其他活动进程的竞争)
1.2 线程的一些属性
- 线程一旦开始执行,IsAlive就是true,线程结束就变成了false。
- 线程结束的条件就是:线程构造函数传入的委托结束了执行。
- 线程一旦结束,就无法重启。
- 每个线程都有一个Name属性,通常用于调试。线程Name只能设置一次,以后更改会执出异常
- 静态的Threac.CurrentThread属性,会返回当前执行的线程。
// 为main线程起个名字
Thread.CurrentThread.Name = "Main Thread...";
//开辟了一个的新的线程Thread
Thread t = new Thread(WryteY);
t.Name = "Y_Thread...";
t.Start();
Console.WriteLine(Thread.CurrentThread.Name);
//主线程在做的一些事
for(int i=0; i<1000; i++)
{
Console.Write("x");
}
void WryteY(object obj)
{
Console.WriteLine(Thread.CurrentThread.Name);
for(int i=0; i < 1000; i++)
{
Console.Write("y");
}
}
1.3 Thread.Join() & Thread.Sleep()
- 调用Join方法,就可以等待另一个线程结束。
Demo1:
Thread t = new Thread(Go);
t.Start();
t.Join();//会等着线程t执行完毕才会接着下面执行
Console.WriteLine("Thread t has ended!");
void Go(object obj)
{
for(int i = 0; i < 1000; i++)
{
Console.Write("y");
}
}
Demo2:
Thread.sleep()被用来暂停当前线程的执行,会通知线程调度器把当前线程在指定的时间周期内置为wait状态。当wait时间结束,线程状态重新变为Runnable并等待CPU的再次调度执行。所以线程sleep的实际时间取决于线程调度器,而这是由操作系统来完成的。
一个进程在运行态时调用sleep(),进入等待态,睡眠结束以后,并不是直接回到运行态,而是进入就绪队列,要等到其他进程放弃时间片后才能重新进入运行态。所以sleep(1000),在1000ms以后,线程不一定会被唤醒。sleep(0)可以看成一个运行态的进程产生一个中断,由运行态直接转入就绪态。这样做是给其他就绪态进程使用时间片的机会。总之,还是操作系统中运行态、就绪态和等待态相互转化的问题。
Thread thread1 = null;
Thread thread2 = null;
thread1 = new Thread(ThreadProc);
thread1.Name = "Thread1";
thread1.Start();
thread2 = new Thread(ThreadProc);
thread2.Name = "Thread2";
thread2.Start();
void ThreadProc(object obj)
{
Console.WriteLine($"Current thread: {
Thread.CurrentThread.Name}");
// 注意ThreadState
if(Thread.CurrentThread.Name != "Thread1" && thread2.ThreadState!=ThreadState.Unstarted)
thread2.Join(); //如果先进来的是thread1,等待thread2执行完毕,去执行thread2的流程
Thread.Sleep(1000); //如果先进来的是thread1,那么此时休眠的是thread2
Console.WriteLine($"Current thread: {
Thread.CurrentThread.Name}");
Console.WriteLine($"Thread1:{
thread1.ThreadState}");
Console.WriteLine($"Thread2:{
thread2.ThreadState}");
}
Demo3: join(超时参数毫秒)
Thread thread1 = null;
Thread thread2 = null;
thread1 = new Thread(ThreadProc);
thread1.Name = "Thread1";
thread1.Start();
thread2 = new Thread(ThreadProc);
thread2.Name = "Thread2";
thread2.Start();
void ThreadProc(object obj)
{
Console.WriteLine($"Current thread: {
Thread.CurrentThread.Name}");
// 注意ThreadState
if(Thread.CurrentThread.Name != "Thread1" && thread2.ThreadState!=ThreadState.Unstarted)
if(thread2