When would I use Task.Yield()?

http://stackoverflow.com/questions/22645024/when-would-i-use-task-yield

In order to have a better understanding for above page, we need first know yield return, below is the code example, put it in a console project, and then build, run to have a look the result:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TestYieldReturn
{
class TestYield
{

/// <summary>
/// 使用 yield 的例子.
/// </summary>
/// <returns></returns>
public IEnumerable<int> GetDataListWithYield()
{
for (int i = 0; i < 5; i++)
{
// 这里模拟一个操作
// 假设 这个方法, 对于每一行数据,都要花费一段时间处理, 才能返回.
Thread.Sleep(1000);
yield return i;
}
}

/// <summary>
/// 不使用 yield 的例子.
/// </summary>
/// <returns></returns>
public IEnumerable<int> GetDataListWithoutYield()
{
List<int> result = new List<int>();
for (int i = 0; i < 5; i++)
{
// 这里模拟一个操作
// 假设 这个方法, 对于每一行数据,都要花费一段时间处理, 才能返回.
Thread.Sleep(1000);
result.Add(i);
}
return result;
}
}

class Program
{
static void Main(string[] args)
{
TestYield test = new TestYield();
Console.WriteLine("测试不使用 yield 的例子!");
Console.WriteLine("==开始时间:{0}", DateTime.Now);
foreach (int data in test.GetDataListWithoutYield())
{
Console.WriteLine("====处理时间:{0}, 处理结果:{1}", DateTime.Now, data);
}
Console.WriteLine("==结束时间:{0}", DateTime.Now);

Console.WriteLine();
Console.WriteLine("测试使用 yield 的例子!");
Console.WriteLine("==开始时间:{0}", DateTime.Now);
foreach (int data in test.GetDataListWithYield())
{
Console.WriteLine("====处理时间:{0}, 处理结果:{1}", DateTime.Now, data);
}
Console.WriteLine("==结束时间:{0}", DateTime.Now);

Console.ReadLine();
}
}
}

Yield Usage Understanding的更多相关文章

  1. Chapter 4: Spring and AOP:Spring's AOP Framework -- draft

    Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...

  2. [转] Understanding JavaScript’s async await

    PS:Promise的用处是异步调用,这个对象使用的时候,call then函数,传一个处理函数进去,处理异步调用后的结果 Promise<Action>这样的对象呢,异步调用后的结果是一 ...

  3. Understanding and Using HRMS Security in Oracle HRMS

    Understanding and Using HRMS Security in Oracle HRMS Product:Oracle Human Resources Minimum Version: ...

  4. Async Performance: Understanding the Costs of Async and Await

    Stephen Toub Download the Code Sample Asynchronous programming has long been the realm of only the m ...

  5. Device Tree Usage( DTS文件语法)

    http://elinux.org/Device_Tree_Usage Device Tree Usage     Top Device Tree page This page walks throu ...

  6. Investigating Your RAM Usage

    转载自:http://developer.android.com/intl/zh-cn/tools/debugging/debugging-memory.html Because Android is ...

  7. Understanding CMS GC Logs--转载

    原文地址:https://blogs.oracle.com/poonam/entry/understanding_cms_gc_logs Understanding CMS GC Logs By Po ...

  8. Understanding Virtual Memory

    Understanding Virtual Memory by Norm Murray and Neil Horman Introduction Definitions The Life of a P ...

  9. (转)The 9 Deep Learning Papers You Need To Know About (Understanding CNNs Part 3)

    Adit Deshpande CS Undergrad at UCLA ('19) Blog About The 9 Deep Learning Papers You Need To Know Abo ...

随机推荐

  1. JVM入门——运行时数据区

    这张图我相信基本上对JVM有点接触的都应该很熟悉,可以说这是JVM入门的第一课.其中的“堆”和“虚拟机栈(栈)”更是耳熟能详.下面将围绕这张图对JVM的运行时数据区做一个简单介绍. 程序计数器(Pro ...

  2. CISCO2960配置vlan

    一.VTP配置 1.VLAN database 2.(VLAN)#vtp domain wx 3.(VLAN)#vtp server 二.VLAN配置 1.VLAN database 2.(VLAN) ...

  3. Unity3D调用摄像头

    代码启用摄像头 .using UnityEngine;   .using System.Collections;   .   .public class WebCamManager : MonoBeh ...

  4. 【SqlServer系列】子查询

    1   概述 1.1  已发布[SqlServer系列]文章 [SqlServer系列]SQLSERVER安装教程 [SqlServer系列]数据库三大范式 [SqlServer系列]表单查询 [Sq ...

  5. Bash : 索引数组

    Bash 提供了两种类型的数组,分别是索引数组(indexed array)和关联数组(associative array).本文主要介绍索引数组的基本用法. 索引数组的基本特点 Bash 提供的数组 ...

  6. CJOJ 2044 【一本通】最长公共子序列(动态规划)

    CJOJ 2044 [一本通]最长公共子序列(动态规划) Description 一个给定序列的子序列是在该序列中删去若干元素后得到的序列.确切地说,若给定序列X,则另一序列Z是X的子序列是指存在一个 ...

  7. new/delete 与 malloc/free的区别

    一.概述     在C++中,申请动态内存与释放动态内存用new/delete 与 malloc/free都可以,而且他们的存储方式相同,new/malloc申请的动态内存位于堆中,无法被操作系统自动 ...

  8. Android - 关于设备版本号

    设备信息可以在Settings - About 里看到 最近想改机器的build number,找到了 build/core/Makefile 里的定义 # Display parameters sh ...

  9. C#获取当前时间详解

    [转]C#获取当前日期时间(转)  http://blog.163.com/ljq086@126/blog/static/549639712010112921658843/ 我们可以通过使用DataT ...

  10. 安装j2ee开发环境

    先安装jdk,再安装eclipse ,再安装myeclipse. eclipse与myeclipse必须在图形化界面安装. 1. 挂载光驱/硬盘 mount  /mnt/cdrom/    挂载光驱 ...