惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
L
LINUX DO - 最新话题
N
News | PayPal Newsroom
S
Security Affairs
W
WeLiveSecurity
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Webroot Blog
Webroot Blog
Spread Privacy
Spread Privacy
A
Arctic Wolf
T
Troy Hunt's Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
Scott Helme
Scott Helme
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
F
Fortinet All Blogs
U
Unit 42
爱范儿
爱范儿
腾讯CDC
S
Security @ Cisco Blogs
PCI Perspectives
PCI Perspectives
Hacker News - Newest:
Hacker News - Newest: "LLM"
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
Security Latest
Security Latest
Y
Y Combinator Blog
S
Schneier on Security
Cisco Talos Blog
Cisco Talos Blog
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
IT之家
IT之家
K
Kaspersky official blog
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 聂微东
Cloudbric
Cloudbric
V
V2EX
H
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
小众软件
小众软件
TaoSecurity Blog
TaoSecurity Blog
T
Tor Project blog
G
Google Developers Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
MyScale Blog
MyScale Blog

博客园 - 广阔之海

Halcon的三角函数 Halcon轮廓插值方法 C# 控件选项变化事件处理 深度学习执行速度不稳定的解决方法 Halcon - 深度学习 - 目标分类 Halcon 画一个时钟 Halcon 解方程(solve_matrix) C# 使用Serilog日志框架 Halcon 生成标定板 Halcon 中的形态学 Halcon的提取中心线算法 C# 消灭switch的面向映射编程 Halcon 通过坐标轴过滤点云数据 Halcon图像投影映射 C# 获取MySql的数据库结构信息 C# HttpClient的使用方法总结 MySql字符集导致特殊字符保存出错问题处理 Asp.Net Core 动态生成WebApi Asp.Net Core WebApi中集成Jwt认证
C#中Task的用法
广阔之海 · 2022-09-12 · via 博客园 - 广阔之海

以下代码是对官方示例的修改:

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            MakeBreakfast01();
            MakeBreakfast02();
            MakeBreakfast03();
            Console.ReadLine();
        }

        // 烤面包、煎蛋、煎肉一样一样做
        public static void MakeBreakfast01()
        {            
            Console.WriteLine("开始做早餐01");
            Stopwatch watch = Stopwatch.StartNew();
            Task.Run(async () =>
            {
                await ToastBreadAsync();
                await FryEggsAsync();
                await FryBaconAsync();
            }).Wait();
            watch.Stop();
            Console.WriteLine($"早餐01完成,耗时{watch.Elapsed.Seconds}秒");
        }

        // 有一个面包机和一个锅,煎蛋、煎肉先后进行,烤面包同时进行
        public static void MakeBreakfast02()
        {            
            Console.WriteLine("开始做早餐02");
            Stopwatch watch = Stopwatch.StartNew();
            var taskToastBread = ToastBreadAsync();
            var taskFryEggsAndBacon = Task.Run(async () =>
            {
                await FryEggsAsync();
                await FryBaconAsync();
            });            
            Task.WaitAll(taskToastBread, taskFryEggsAndBacon);
            watch.Stop();
            Console.WriteLine($"早餐02完成,耗时{watch.Elapsed.Seconds}秒");
        }

        // 有一个面包机和两个锅,烤面包、煎蛋、煎肉三项同时进行
        public static void MakeBreakfast03()
        {            
            Console.WriteLine("开始做早餐03");
            Stopwatch watch = Stopwatch.StartNew();
            var taskToastBread = ToastBreadAsync();
            var taskFryEggs = FryEggsAsync();
            var taskFryBacon = FryBaconAsync();
            Task.WaitAll(taskToastBread, taskFryEggs, taskFryBacon);
            watch.Stop();
            Console.WriteLine($"早餐03完成,耗时{watch.Elapsed.Seconds}秒");
        }

        // 煎蛋
        public static async Task FryEggsAsync()
        {
            Console.WriteLine("开始煎蛋");
            await Task.Delay(1000);
            Console.WriteLine("煎蛋完成");
        }

        // 煎肉
        public static async Task FryBaconAsync()
        {
            Console.WriteLine("开始煎肉");
            await Task.Delay(1000);
            Console.WriteLine("煎肉完成");
        }

        // 烤面包
        public static async Task ToastBreadAsync()
        {
            Console.WriteLine("开始烤面包");
            await Task.Delay(1000);
            Console.WriteLine("烤面包完成");
        }
    }
}

运行结果:

posted @ 2022-09-12 08:02  广阔之海  阅读(145)  评论()    收藏  举报