Yield Usage Understanding
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的更多相关文章
- 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 ...
- [转] Understanding JavaScript’s async await
PS:Promise的用处是异步调用,这个对象使用的时候,call then函数,传一个处理函数进去,处理异步调用后的结果 Promise<Action>这样的对象呢,异步调用后的结果是一 ...
- Understanding and Using HRMS Security in Oracle HRMS
Understanding and Using HRMS Security in Oracle HRMS Product:Oracle Human Resources Minimum Version: ...
- 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 ...
- Device Tree Usage( DTS文件语法)
http://elinux.org/Device_Tree_Usage Device Tree Usage Top Device Tree page This page walks throu ...
- Investigating Your RAM Usage
转载自:http://developer.android.com/intl/zh-cn/tools/debugging/debugging-memory.html Because Android is ...
- Understanding CMS GC Logs--转载
原文地址:https://blogs.oracle.com/poonam/entry/understanding_cms_gc_logs Understanding CMS GC Logs By Po ...
- Understanding Virtual Memory
Understanding Virtual Memory by Norm Murray and Neil Horman Introduction Definitions The Life of a P ...
- (转)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 ...
随机推荐
- 单片机C语言基础编程源码六则2
1.某单片机系统的P2口接一数模转换器DAC0832输出模拟量,现在要求从DAC0832输出连续的三角波,实现的方法是从P2口连续输出按照三角波变化的数值,从0开始逐渐增大,到某一最大值后逐渐减小,直 ...
- [bzoj 2243]: [SDOI2011]染色 [树链剖分][线段树]
Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“ ...
- sql操作一般函数
sql操作一般函数 函数一般语法:SELECT function(列) FROM 表 函数的基本类型是: Aggregate 合计函数:函数的操作面向一系列的值,并返回一个单一的值. Scalar 函 ...
- ant在持续集成的应用
一.Ant概述 Ant是一个Apache基金会下的跨平台的构建工具,它是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具. 在本文中,我主要让介绍Ant的命令.构建文件.最后部分以一个实 ...
- CAP 介绍及使用【视频】
前言 很多同学可能对于CAP这个项目想有更一步的了解,包括她的工作原理以及适用的场景,所以博主就准备了一场直播给他家讲解了一下,这个视频是直播的一个录像. 由于我这次直播本来是没有打算对外的,所以也是 ...
- 高效搭建lnmp环境
1:安装nginx sudo apt-get install nginx 检测 nginx : sudo nginx -t 出现如下表示成功 2:安装配置m ...
- git中使用命令将远程仓库拉取项目在本地文件夹
在有些时候,我们往往从github或者gitlab或者coding上面直接下载项目下来运行,但是这种情况往往没有使用git远程拉取来的安全(或者叫装逼), 所以这里我以gitLab为例子,说一下如何将 ...
- TCP 滑动窗口
滑动窗口协议 流量控制方法 PUSH 慢启动 隔一个报文段确认"的策略实际就是因为 delayed ack,同时接收到两个待确认的ACK包时,就立即发送确认包. 滑动窗口实例 解 ...
- Android对话框和帧动画
Android对话框 在一个例子中展示四种对话框. 设置四个按钮 <LinearLayout xmlns:android="http://schemas.android.com/apk ...
- Java集合框架Collections【List/Set】
1.基本介绍: 集合就是存放对象的,他比数组好的一点就是他一开始不清楚自己长度 容器一般是分为很多种的,很多的容器在一起然后进过断的抽象和抽取就成了一个体系,我们称之为集合框架 我们看体系首先是看顶层 ...