有两种代码变体,其中秒表以不同的方式测量经过的时间。我不明白为什么。这是代码 -
1.第一个例子(在AsyncAction方法中,同步部分在前,然后是异步部分)-
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace DifferentTasks
{
public class SyncAndAsyncPartsOfMethod
{
public async Task Execute()
{
IEnumerable<int> data = Enumerable.Range(0, 100);
Stopwatch stopwatch = Stopwatch.StartNew();
Task t = Task.WhenAll(data.Select(AsyncAction));
await t;
stopwatch.Stop();
double elapsedSeconds = TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
Console.WriteLine("Elapsed seconds overall - " + elapsedSeconds);
}
private async Task AsyncAction(int x)
{
Stopwatch stopwatch = Stopwatch.StartNew();
Thread.Sleep(TimeSpan.FromSeconds(1));
await Task.Delay(TimeSpan.FromSeconds(1));
stopwatch.Stop();
double elapsedSeconds = TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
Console.WriteLine($"AsyncAction {x} is finished, elapsed seconds - {elapsedSeconds}, thread - {Thread.CurrentThread.ManagedThreadId}");
}
}
}
结论:总经过秒数 - 102 秒
AsyncAction 方法中的秒表显示该方法每次运行正好两秒:
AsyncAction 95 is finished, elapsed seconds - 2,046, thread - 5
AsyncAction 96 is finished, elapsed seconds - 2,032, thread - 4
AsyncAction 97 is finished, elapsed seconds - 2,015, thread - 4
AsyncAction 98 is finished, elapsed seconds - 2,029, thread - 4
AsyncAction 99 is finished, elapsed seconds - 2,046, thread - 5
2.第二个例子(在AsyncAction方法中,异步部分在前,然后是同步部分)-
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace DifferentTasks
{
public class SyncAndAsyncPartsOfMethod
{
public async Task Execute()
{
IEnumerable<int> data = Enumerable.Range(0, 100);
Stopwatch stopwatch = Stopwatch.StartNew();
Task t = Task.WhenAll(data.Select(AsyncAction));
await t;
stopwatch.Stop();
double elapsedSeconds = TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
Console.WriteLine("Elapsed seconds overall - " + elapsedSeconds);
}
private async Task AsyncAction(int x)
{
Stopwatch stopwatch = Stopwatch.StartNew();
await Task.Delay(TimeSpan.FromSeconds(1));
Thread.Sleep(TimeSpan.FromSeconds(1));
stopwatch.Stop();
double elapsedSeconds = TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
Console.WriteLine($"AsyncAction {x} is finished, elapsed seconds - {elapsedSeconds}, thread - {Thread.CurrentThread.ManagedThreadId}");
}
}
}
结论:总经过秒数 - 13 秒
AsyncAction 方法中的秒表现在显示 AsyncAction 方法每次执行所需的时间更长 -
AsyncAction 92 is finished, elapsed seconds - 3,066, thread - 6
AsyncAction 89 is finished, elapsed seconds - 4,035, thread - 8
AsyncAction 87 is finished, elapsed seconds - 4,082, thread - 6
AsyncAction 85 is finished, elapsed seconds - 4,082, thread - 5
AsyncAction 83 is finished, elapsed seconds - 5,051, thread - 8
AsyncAction 81 is finished, elapsed seconds - 5,067, thread - 4
启动这些课程的代码 -
using System;
using System.Runtime.InteropServices;
namespace DifferentTasks
{
internal class Program
{
public static void Main(string[] args)
{
SyncAndAsyncPartsOfMethod syncAndAsyncPartsOfMethod = new();
syncAndAsyncPartsOfMethod.Execute().Wait();
Console.ReadLine();
}
}
}
我想了解为什么 -
- 根据是先执行异步部分还是先执行同步部分,总运行时间会有所不同
- 为什么先执行异步部分,方法的执行时间每次都会增加。
我会尝试用手指来解释。
在继续编写代码之前,您需要了解以下内容:
Thread.Sleep(TimeSpan.FromSeconds(1));会阻塞当前线程。也就是说,在该操作结束之前,该线程无法执行任何操作。await Task.Delay(TimeSpan.FromSeconds(1));不会阻止任何内容。它可能在一个线程中启动,但后续部分(此行之后的所有内容)将在另一个线程中执行。现在让我们看一下代码。假设你在线程A中调用它,也就是说,程序本身运行在线程A中,这是
Task t = Task.WhenAll(data.Select(AsyncAction));在线程A中调用的,相应地,方法中await之前的所有内容AsyncAction也在线程A中调用。这是你的方法,让我们弄清楚这里发生了什么:
现在让我们看看另一种选择
要了解与线程池的连接,您可以在程序的一开始就指定所需的线程数,例如这个
将允许第二个选项运行 2 秒,但不会以任何方式影响第一个选项。
如果我们打个比方:
假设有100人,每个人都需要去药房买药。
在第一种情况下,所有 100 个人都排成一条线,与一位开药的治疗师坐在一起(主流 A),然后分散到药房(在池中流动)。也就是说,如果治疗师在一个人身上花费1秒,那么他就会在整条线上花费100秒。但随后他为排队的第一个医生开出了处方,并邀请了第二个医生,而治疗师为第二个医生开了处方,第一个医生则去药房购买他需要的东西。当第二个离开医生时,第一个已经买好了药。等等。原来,当第100个离开医生时,前面的人都已经买好了药,第100个花了1秒给自己买了一颗。瞧,每个人的总时间约为 101 秒。
在第二种情况下,100 名患者同时获得公共服务,为自己开出处方(1 秒内),并成群结队地前往城市中的不同药店(一家药店 - 一家从池中流出)。每个药店都会有自己的队列。如果全市只有3家药店,有100名患者,那么每家药店大约有33人排队。因此,有人会很幸运,在政府服务上花 1 秒,在药房排队再花 1 秒。而对于一些人来说,没有那么多,他会在政府服务上花1秒,但会在药房排队等待33秒。但最终,大约 35 秒后,所有 100 名患者都会拿到药物。