zoukankan      html  css  js  c++  java
  • 基于任务的异步编程模型 (TAP) 实现做早餐示例代码

    using System;
    using System.Threading.Tasks;
    
    namespace AsyncBreakfast
    {
    class Program
    {
    static void Main(string[] args)
    {
    Coffee cup = PourCoffee();
    Console.WriteLine("coffee is ready");
    
    Egg eggs = FryEggs(2);
    Console.WriteLine("eggs are ready");
    
    Bacon bacon = FryBacon(3);
    Console.WriteLine("bacon is ready");
    
    Toast toast = ToastBread(2);
    ApplyButter(toast);
    ApplyJam(toast);
    Console.WriteLine("toast is ready");
    
    Juice oj = PourOJ();
    Console.WriteLine("oj is ready");
    Console.WriteLine("Breakfast is ready!");
    }
    
    private static Juice PourOJ()
    {
    Console.WriteLine("Pouring orange juice");
    return new Juice();
    }
    
    private static void ApplyJam(Toast toast) =>
    Console.WriteLine("Putting jam on the toast");
    
    private static void ApplyButter(Toast toast) =>
    Console.WriteLine("Putting butter on the toast");
    
    private static Toast ToastBread(int slices)
    {
    for (int slice = 0; slice < slices; slice++)
    {
    Console.WriteLine("Putting a slice of bread in the toaster");
    }
    Console.WriteLine("Start toasting...");
    Task.Delay(3000).Wait();
    Console.WriteLine("Remove toast from toaster");
    
    return new Toast();
    }
    
    private static Bacon FryBacon(int slices)
    {
    Console.WriteLine($"putting {slices} slices of bacon in the pan");
    Console.WriteLine("cooking first side of bacon...");
    Task.Delay(3000).Wait();
    for (int slice = 0; slice < slices; slice++)
    {
    Console.WriteLine("flipping a slice of bacon");
    }
    Console.WriteLine("cooking the second side of bacon...");
    Task.Delay(3000).Wait();
    Console.WriteLine("Put bacon on plate");
    
    return new Bacon();
    }
    
    private static Egg FryEggs(int howMany)
    {
    Console.WriteLine("Warming the egg pan...");
    Task.Delay(3000).Wait();
    Console.WriteLine($"cracking {howMany} eggs");
    Console.WriteLine("cooking the eggs ...");
    Task.Delay(3000).Wait();
    Console.WriteLine("Put eggs on plate");
    
    return new Egg();
    }
    
    private static Coffee PourCoffee()
    {
    Console.WriteLine("Pouring coffee");
    return new Coffee();
    }
    }
    }

    来源:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/async/

  • 相关阅读:
    Linux学习总结(16)——CentOS 下 Nginx + Tomcat 配置负载均衡
    photo的复数是photos
    APUE1.11:系统调用 库函数
    Linux的man手册共有以下几个章节
    [关于宝宝的一些网上摘抄]
    ZT-Android深入浅出之Binder机 制
    jclass和jobject的迷惑
    ZT JAVA WeakReference
    ZT————pull push mode
    看了xici有写给孩子的信,maybe我也要写给孩子一些东西了
  • 原文地址:https://www.cnblogs.com/pandola/p/14818785.html
Copyright © 2011-2022 走看看