Is Task == Lazy?

前端 未结 3 1159
予麋鹿
予麋鹿 2021-01-21 05:46
public Data GetCurrent(Credentials credentials)
{ 
    var data = new Lazy(() => GetCurrentInternal(credentials));
    try
    {
        return data.Value         


        
相关标签:
3条回答
  • 2021-01-21 05:59

    Similarities

    Both Lazy<T> and Task<T> promise to do some work later and return a result of type T.

    Differences

    Lazy<T> promises to do its work as late as possible if it is required at all, and does so synchronously.

    Task<T> however can do its work asynchronously while your thread does other work, or blocks awaiting result.

    Lazy<T> will bubble up any exception caused by the lambda when you call .Value.

    Task<T> will keep any exception caused by the lambda and throw it later when you await task. Or if you task.Wait() it may wrap the exception in an AggregationException. I refer you to this for more info on catching exceptions thrown by tasks: Catch an exception thrown by an async method and this http://stiller.co.il/blog/2012/12/task-wait-vs-await/

    0 讨论(0)
  • 2021-01-21 06:06

    No, Lazy<T> is not the same as Task<T>.


    Lazy<T> is an implementation of the concept of lazy evaluation:

    1. It represents a T value that will be computed synchronously at first access, which may or may not occur at all.
    2. Evaluation happens at first access, and subsequent accesses use a cached value. Memoization is a closely related concept.
    3. The first access will block in order to compute the value, while subsequent accesses will yield the value immediately.

    Task<T> is related to the concept of a future.

    1. It represents a T value that will be computed (typically asynchronously) at the creation of its promise, even if nobody ends up actually accessing it.
    2. All accesses yield a cached value that has been or will be computed by the evaluation mechanism (the promise) at a prior or future time.
    3. Any access that occurs before the computation of the value has finished will block until the calculation is complete. Any access that occurs after the computation of the value has finished will immediately yield the computed value.

    All that being said, Lazy<T> and Task<T> do have something in common that you may have already picked up.

    Each of these types is a unary generic type that describes a unique way to perform a particular computation that would yield a T value (and that computation can conveniently be passed as a delegate or lambda). In other words, each of these types is an example of a monad. You can find some very good and simple explanations of what a monad is here on Stack Overflow and elsewhere.

    0 讨论(0)
  • 2021-01-21 06:10

    Task and Lazy are completely different concepts.

    You use Task to do asynchronous operations. Some boring MSDN:

    The Task class represents a single operation that does not return a value and that usually executes asynchronously. Task objects are one of the central components of the task-based asynchronous pattern first introduced in the .NET Framework 4. Because the work performed by a Task object typically executes asynchronously on a thread pool thread rather than synchronously on the main application thread, you can use the Status property, as well as the IsCanceled, IsCompleted, and IsFaulted properties, to determine the state of a task. Most commonly, a lambda expression is used to specify the work that the task is to perform.

    Lazy is used for deferred initialization of an object. It means, that your object gets initialized only when you call lazyObj.Value.

    Use lazy initialization to defer the creation of a large or resource-intensive object, or the execution of a resource-intensive task, particularly when such creation or execution might not occur during the lifetime of the program.

    To prepare for lazy initialization, you create an instance of Lazy. The type argument of the Lazy object that you create specifies the type of the object that you want to initialize lazily. The constructor that you use to create the Lazy object determines the characteristics of the initialization. Lazy initialization occurs the first time the Lazy.Value property is accessed.

    0 讨论(0)
提交回复
热议问题