zoukankan      html  css  js  c++  java
  • await Task.CompletedTask vs return

    I'm trying to understand the difference between await Task.CompletedTask and return but can't seem to find any clearly defined explanation.

    Why / when would you use this:

    public async Task Test()
    {
        await Task.CompletedTask;
    }

    over this?

    public async Task Test()
    {
        return;
    }

    Let's look the question from the consumer-side.

    If you define an interface, which imposes an operation that returns a Task then you actually don't say anything about how it will be calculated / executed (so, there is no async access modifier in the method signature). It is an implementation detail.

    Interface

    public interface ITest
    {
        Task Test();
        Task<bool> IsTest();
    }

    So, it is up to you how you implement the interface.

    You can do it in a synchronous way, when there won't be any AsyncStateMachine generated because of the absence of the async keyword.

    Implementation #1

    public class TestImpl : ITest
    {
        public Task Test()
        {
            return Task.CompletedTask;
        }
    
        public Task<bool> IsTest()
        {
            return Task.FromResult(true);
        }
    }

    Let's look the question from the consumer-side.

    If you define an interface, which imposes an operation that returns a Task then you actually don't say anything about how it will be calculated / executed (so, there is no async access modifier in the method signature). It is an implementation detail.

    Interface

    public interface ITest
    {
        Task Test();
        Task<bool> IsTest();
    }
    

    So, it is up to you how you implement the interface.

    You can do it in a synchronous way, when there won't be any AsyncStateMachine generated because of the absence of the async keyword.

    Implementation #1

    public class TestImpl : ITest
    {
        public Task Test()
        {
            return Task.CompletedTask;
        }
    
        public Task<bool> IsTest()
        {
            return Task.FromResult(true);
        }
    }
    

    Or you can try to implement it in an asynchronous way but without await operators. Here you will receive CS1998 warnings.

    Implementation #2

    public class TestImpl : ITest
    {
        public async Task Test()
        {
            return;
        }
    
        public async Task<bool> IsTest()
        {
            return true;
        }
    }

    This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

    In other words this implementation does not define a state machine. An async method is divided into different states based on the await keywords:

    • before_the_await_1,
    • after_the_await_1_but_before_the_await_2
    • after_the_await_2_but_before_the_await_3
    • ...

    If you haven't got any await then you would have a single state, which would run in sync (there is no need to preserve state, execute async operation and then call MoveNext()).

    Or you can try to implement it in an asynchronous way with await operators.

    Implementation #3

    public class TestImpl : ITest
    {
        public async Task Test()
        {
            await Task.CompletedTask;
        }
    
        public async Task<bool> IsTest()
        {
            return await Task.FromResult(true);
        }
    }

    In this case there will be an async state machine but it won't be allocated on the heap. By default it is a struct and if it finishes in sync then we don't need to allocate them on the heap to extend its life out of the scope of the method.

    For further information please read these articles:

    转:https://stackoverflow.com/questions/63885702/await-task-completedtask-vs-return

  • 相关阅读:
    366. Find Leaves of Binary Tree输出层数相同的叶子节点
    716. Max Stack实现一个最大stack
    515. Find Largest Value in Each Tree Row查找一行中的最大值
    364. Nested List Weight Sum II 大小反向的括号加权求和
    156. Binary Tree Upside Down反转二叉树
    698. Partition to K Equal Sum Subsets 数组分成和相同的k组
    244. Shortest Word Distance II 实现数组中的最短距离单词
    187. Repeated DNA Sequences重复的DNA子串序列
    java之hibernate之基于主键的双向一对一关联映射
    java之hibernate之基于主键的单向一对一关联映射
  • 原文地址:https://www.cnblogs.com/superfeeling/p/15322970.html
Copyright © 2011-2022 走看看