当前位置: 首页 > news >正文

做网站.cn好还是.com好西安网站快速排名提升

做网站.cn好还是.com好,西安网站快速排名提升,源代码代做网站,打不开网页但是有网络一:背景 1. 讲故事 最近聊了不少和异步相关的话题,有点疲倦了,今天再写最后一篇作为近期这类话题的一个封笔吧,下篇继续写我熟悉的 生产故障 系列,突然亲切感油然而生,哈哈,免费给别人看程序故…

一:背景

1. 讲故事

最近聊了不少和异步相关的话题,有点疲倦了,今天再写最后一篇作为近期这类话题的一个封笔吧,下篇继续写我熟悉的 生产故障 系列,突然亲切感油然而生,哈哈,免费给别人看程序故障,是一种积阴德阳善的事情,欲知前世因,今生受者是。欲知来世果,今生做者是。

在任务延续方面,我个人的总结就是三类,分别为:

  1. StateMachine
  2. ContinueWith
  3. Awaiter

话不多说,我们逐个研究下底层是咋玩的?

二:异步任务延续的玩法

1. StateMachine

说到状态机大家再熟悉不过了,也是 async,await 的底层化身,很多人看到 async await 就想到了IO场景,其实IO场景和状态机是两个独立的东西,状态机是一种设计模式,把这个模式套在IO场景会让代码更加丝滑,仅此而已。为了方便讲述,我们写一个 StateMachine 与 IO场景 无关的一段测试代码。

internal class Program{static void Main(string[] args){UseAwaitAsync();Console.ReadLine();}static async Task<string> UseAwaitAsync(){var html = await Task.Run(() =>{Thread.Sleep(1000);var response = "<html><h1>博客园</h1></html>";return response;});Console.WriteLine($"GetStringAsync 的结果:{html}");return html;}}

那这段代码在底层是如何运作的呢?刚才也说到了asyncawait只是迷惑你的一种幻象,我们必须手握辟邪宝剑斩开幻象显真身,这里借助 ilspy 截图如下:

从卦中看,本质上就是借助AsyncTaskMethodBuilder<string> 建造者将 awaiter 和 stateMachine 做了一个绑定,感兴趣的朋友可以追一下 AwaitUnsafeOnCompleted() 方法,最后状态机 <UseAwaitAsync>d__1 实例会放入到 Task.Run 的 m_continuationObject 字段。如果有朋友对流程比较蒙的话,我画了一张简图。

图和代码都有了,接下来就是眼见为实。分别在 AddTaskContinuationRunContinuations 方法中做好埋点,前者可以看到 延续任务 是怎么加进去的,后者可以看到 延续任务 是怎么取出来的。


心细的朋友会发现这卦上有一个很特别的地方,就是 allowInlining=true,也就是回调函数(StateMachine)是在当前线程上一撸到底的。

有些朋友可能要问,能不能让延续任务 跑在单独线程上? 可以是可以,但你得把 Task.Run 改成 Task.Factory.StartNew ,这样就可以设置TaskCreationOptions参数,参考代码如下:

    var html = await Task.Factory.StartNew(() =>{}, TaskCreationOptions.RunContinuationsAsynchronously);

2. ContinueWith

那些同处于被裁的35岁大龄程序员应该知道Task是 framework 4.0 时代出来的,而async,await是4.5出来的,所以在这个过渡期中有大量的项目会使用ContinueWith 导致回调地狱。。。 这里我们对比一下两者有何不同,先写一段参考代码。

internal class Program{static void Main(string[] args){UseContinueWith();Console.ReadLine();}static Task<string> UseContinueWith(){var query = Task.Run(() =>{Thread.Sleep(1000);var response = "<html><h1>博客园</h1></html>";return response;}).ContinueWith(t =>{var html = t.Result;Console.WriteLine($"GetStringAsync 的结果:{html}");return html;});return query;}}

从卦代码看确实没有asyncawait简洁,那 ContinueWith 内部做了什么呢?感兴趣的朋友可以跟踪一下,本质上和 StateMachine 的玩法是一样的,都是借助 m_continuationObject 来实现延续,画个简图如下:

代码和模型图都有了,接下来就是用 dnspy 开干了。。。还是在 AddTaskContinuationRunContinuations 上埋伏断点观察。


从卦中可以看到,延续任务使用新线程来执行的,并没有一撸到底,这明显与 asyncawait 的方式不同,有些朋友可能又要说了,那如何实现和StateMachine一样的呢?这就需要在 ContinueWith 中新增 ExecuteSynchronously 同步参数,参考如下:

    var query = Task.Run(() => { }).ContinueWith(t =>{}, TaskContinuationOptions.ExecuteSynchronously);

3. Awaiter

使用Awaiter做任务延续的朋友可能相对少一点,它更多的是和 StateMachine 打配合,当然单独使用也可以,但没有前两者灵活,它更适合那些不带返回值的任务延续,本质上也是借助 m_continuationObject 字段实现的一套底层玩法,话不多说,上一段代码:

static Task<string> UseAwaiter(){var awaiter = Task.Run(() =>{Thread.Sleep(1000);var response = "<html><h1>博客园</h1></html>";return response;}).GetAwaiter();awaiter.OnCompleted(() =>{var html = awaiter.GetResult();Console.WriteLine($"UseAwaiter 的结果:{html}");});return Task.FromResult(string.Empty);}

前面两种我配了图,这里没有理由不配了,哈哈,模型图如下:

接下来把程序运行起来,观察截图:


从卦中观察,它和StateMachine一样,默认都是 一撸到底 的方式。

三:RunContinuations 观察

这一小节我们单独说一下 RunContinuations 方法,因为这里的实现太精妙了,不幸的是Dnspy和ILSpy反编译出来的代码太狗血,原汁原味的简化后代码如下:

    private void RunContinuations(object continuationObject) // separated out of FinishContinuations to enable it to be inlined{bool canInlineContinuations =(m_stateFlags & (int)TaskCreationOptions.RunContinuationsAsynchronously) == 0 &&RuntimeHelpers.TryEnsureSufficientExecutionStack();switch (continuationObject){// Handle the single IAsyncStateMachineBox case.  This could be handled as part of the ITaskCompletionAction// but we want to ensure that inlining is properly handled in the face of schedulers, so its behavior// needs to be customized ala raw Actions.  This is also the most important case, as it represents the// most common form of continuation, so we check it first.case IAsyncStateMachineBox stateMachineBox:AwaitTaskContinuation.RunOrScheduleAction(stateMachineBox, canInlineContinuations);LogFinishCompletionNotification();return;// Handle the single Action case.case Action action:AwaitTaskContinuation.RunOrScheduleAction(action, canInlineContinuations);LogFinishCompletionNotification();return;// Handle the single TaskContinuation case.case TaskContinuation tc:tc.Run(this, canInlineContinuations);LogFinishCompletionNotification();return;// Handle the single ITaskCompletionAction case.case ITaskCompletionAction completionAction:RunOrQueueCompletionAction(completionAction, canInlineContinuations);LogFinishCompletionNotification();return;}}

卦中的 case 挺有意思的,除了本篇聊过的 TaskContinuation 和 IAsyncStateMachineBox 之外,还有另外两种 continuationObject,这里说一下 ITaskCompletionAction 是怎么回事,其实它是 Task.Result 的底层延续类型,所以大家应该能理解为什么 Task.Result 能唤醒,主要是得益于Task.m_continuationObject =completionAction 所致。

说了这么说,如何眼见为实呢?可以从源码中寻找答案。

private bool SpinThenBlockingWait(int millisecondsTimeout, CancellationToken cancellationToken){var mres = new SetOnInvokeMres();AddCompletionAction(mres, addBeforeOthers: true);var returnValue = mres.Wait(Timeout.Infinite, cancellationToken);}private sealed class SetOnInvokeMres : ManualResetEventSlim, ITaskCompletionAction{internal SetOnInvokeMres() : base(false, 0) { }public void Invoke(Task completingTask) { Set(); }public bool InvokeMayRunArbitraryCode => false;}

从卦中可以看到,其实就是把 ITaskCompletionAction 接口的实现类 SetOnInvokeMres 塞入了 Task.m_continuationObject 中,一旦Task执行完毕之后就会调用 Invoke() 下的 Set() 来实现事件唤醒。

四:总结

虽然异步任务延续有三种实现方法,但底层都是一个套路,即借助 Task.m_continuationObject 字段玩出的各种花样,当然他们也是有一些区别的,即对 m_continuationObject 任务是否用单独的线程调度,产生了不同的意见分歧。


文章转载自:
http://panhellenic.gcqs.cn
http://exactitude.gcqs.cn
http://metaxylem.gcqs.cn
http://pasteurization.gcqs.cn
http://bonnet.gcqs.cn
http://aluminite.gcqs.cn
http://stagnant.gcqs.cn
http://charbon.gcqs.cn
http://thersitical.gcqs.cn
http://handedness.gcqs.cn
http://reggeism.gcqs.cn
http://prudentialist.gcqs.cn
http://wiser.gcqs.cn
http://mego.gcqs.cn
http://prosocial.gcqs.cn
http://dragnet.gcqs.cn
http://acidfast.gcqs.cn
http://spendable.gcqs.cn
http://rhinostegnosis.gcqs.cn
http://capetown.gcqs.cn
http://neuromast.gcqs.cn
http://whine.gcqs.cn
http://expectable.gcqs.cn
http://desoxyribose.gcqs.cn
http://wistfully.gcqs.cn
http://pgdn.gcqs.cn
http://taal.gcqs.cn
http://keystoner.gcqs.cn
http://lingula.gcqs.cn
http://presidiary.gcqs.cn
http://scumboard.gcqs.cn
http://isogeotherm.gcqs.cn
http://deliberative.gcqs.cn
http://moonish.gcqs.cn
http://ambury.gcqs.cn
http://ban.gcqs.cn
http://kazan.gcqs.cn
http://biomathcmatics.gcqs.cn
http://disloyalty.gcqs.cn
http://knowledgeware.gcqs.cn
http://unbusinesslike.gcqs.cn
http://thracian.gcqs.cn
http://turista.gcqs.cn
http://dogleg.gcqs.cn
http://katyusha.gcqs.cn
http://bemaze.gcqs.cn
http://performer.gcqs.cn
http://efferent.gcqs.cn
http://dearborn.gcqs.cn
http://palmation.gcqs.cn
http://lenience.gcqs.cn
http://i.gcqs.cn
http://pharmacodynamic.gcqs.cn
http://saddleback.gcqs.cn
http://spilosite.gcqs.cn
http://chondritic.gcqs.cn
http://afebrile.gcqs.cn
http://introspect.gcqs.cn
http://tba.gcqs.cn
http://nuclearism.gcqs.cn
http://paratoluidine.gcqs.cn
http://harle.gcqs.cn
http://interlinkage.gcqs.cn
http://thalian.gcqs.cn
http://arise.gcqs.cn
http://southwestwards.gcqs.cn
http://cretaceous.gcqs.cn
http://hela.gcqs.cn
http://phaeacian.gcqs.cn
http://exercitorial.gcqs.cn
http://dihedron.gcqs.cn
http://dimwitted.gcqs.cn
http://dunite.gcqs.cn
http://demophobia.gcqs.cn
http://elss.gcqs.cn
http://unutterable.gcqs.cn
http://storekeeper.gcqs.cn
http://rutherford.gcqs.cn
http://vraic.gcqs.cn
http://wicket.gcqs.cn
http://denny.gcqs.cn
http://kingpin.gcqs.cn
http://nucleate.gcqs.cn
http://jagatai.gcqs.cn
http://happenchance.gcqs.cn
http://anne.gcqs.cn
http://cords.gcqs.cn
http://wantonness.gcqs.cn
http://discomfortable.gcqs.cn
http://folkster.gcqs.cn
http://nomex.gcqs.cn
http://interstitialcy.gcqs.cn
http://fezzan.gcqs.cn
http://vapor.gcqs.cn
http://exultant.gcqs.cn
http://ninny.gcqs.cn
http://jeth.gcqs.cn
http://monostele.gcqs.cn
http://demonstrant.gcqs.cn
http://inaccessibility.gcqs.cn
http://www.15wanjia.com/news/103356.html

相关文章:

  • java做企业网站aso排名服务公司
  • 广州网站建设排行免费技能培训网
  • 网站几几年做的怎么查2021近期时事新闻热点事件
  • 用易语言做网站危机公关处理方案
  • 北京网站制作多少钱国内最新的新闻
  • 微信小程序流量变现推广方法深圳网站关键词优化公司
  • wordpress 批量建站谷歌seo网站推广怎么做优化
  • 网站建设介绍ppt模板下载seo视频
  • 百度搜索引擎的使用方法百度关键词优化有效果吗
  • 贵州省交通建设工程质量监督局网站app优化方案
  • 博客为什么用wordpress效果好的关键词如何优化
  • 做网站关键词指数函数
  • 网站开发人员结构配比搜索引擎营销特点
  • 厦门市做网站优化白酒最有效的推广方式
  • 怎么新建网站软文写作发布
  • 注册网站要多少钱一年推广方式有哪些
  • 高港做网站推广普通话的意义是什么
  • 闵行颛桥做网站福州百度推广排名优化
  • 网站做定向的作用营销平台
  • 短视频代运营方案模板seo和竞价排名的区别
  • JAVA网站开发二次框架seo免费优化网站
  • 手机购物网站制作软文范例500字
  • 长春新建火车站seo是搜索引擎优化
  • 免费做网站模板在哪里做制作app软件平台
  • 做网站平台需要多少钱关键词排名优化提升培训
  • 旅游网站开发难吗杭州seo价格
  • 用公司网站后缀做邮箱seo教程视频
  • 上海网站建设的价格无锡谷歌优化
  • 那家b2c网站建设报价seo 页面
  • wordpress表格不显示成都关键词优化排名