博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#多线程Task的WhenAll用法
阅读量:4092 次
发布时间:2019-05-25

本文共 2345 字,大约阅读时间需要 7 分钟。

多个task调度的实例,需要加锁lock

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleAppTest{    class Program    {        static void Main(string[] args)        {            var account = new Account(1000);            var tasks = new Task[100];            for (int i = 0; i < tasks.Length; i++)            {                tasks[i] = Task.Run(() => Update(account));            }            Task t = Task.WhenAll(tasks);            try            {                t.Wait();            }            catch { }            if (t.Status == TaskStatus.RanToCompletion)                Console.WriteLine("All ping attempts succeeded.");            else if (t.Status == TaskStatus.Faulted)                Console.WriteLine("ping attempts failed");            Console.WriteLine($"Account's balance is {account.GetBalance()}");            Console.ReadKey();        }        static void Update(Account account)        {            decimal[] amounts = { 0, 2, -3, 6, -2, -1, 8, -5, 11, -6 };            foreach (var amount in amounts)            {                if (amount >= 0)                {                    account.Credit(amount);                }                else                {                    account.Debit(Math.Abs(amount));                }            }        }    }    }public class Account{    private readonly object balanceLock = new object();    private decimal balance;    public Account(decimal initialBalance) => balance = initialBalance;    public decimal Debit(decimal amount)    {        if (amount < 0)        {            throw new ArgumentOutOfRangeException(nameof(amount), "The debit amount cannot be negative.");        }        decimal appliedAmount = 0;        lock (balanceLock)        {            if (balance >= amount)            {                balance -= amount;                appliedAmount = amount;            }        }        return appliedAmount;    }    public void Credit(decimal amount)    {        if (amount < 0)        {            throw new ArgumentOutOfRangeException(nameof(amount), "The credit amount cannot be negative.");        }        lock (balanceLock)        {            balance += amount;        }    }    public decimal GetBalance()    {        lock (balanceLock)        {            return balance;        }    }}

参考:

转载地址:http://okiii.baihongyu.com/

你可能感兴趣的文章
phpquery抓取网站内容简单介绍
查看>>
找工作准备的方向(4月22日写的)
查看>>
关于fwrite写入文件后打开查看是乱码的问题
查看>>
用结构体指针前必须要用malloc,不然会出现段错误
查看>>
Linux系统中的美
查看>>
一些实战项目(linux应用层编程,多线程编程,网络编程)
查看>>
原来k8s docker是用go语言写的,和现在所讲的go是一个东西!
查看>>
STM32CubeMX 真的不要太好用
查看>>
不要买铝合金机架的无人机,不耐摔,易变形弯曲。
查看>>
ACfly也是基于FreeRTOS的
查看>>
我发现七月在线的GAAS课程基本都讲到了
查看>>
电机堵转
查看>>
carzepony也在想往FreeRTOS上迁移
查看>>
可以买个好点的电烙铁
查看>>
ACfly调参记录(包括ACfly-F330和ACfly-T265)
查看>>
一定记得每飞几次或者隔一天要把螺丝和浆帽拧一次,确实会松的
查看>>
《多旋翼无人飞行器嵌入式飞控开发指南》里基于FreeRTOS的无人机软件框架
查看>>
思岚A1的SDK其实很好读懂,每个函数清晰明了,可以直接调用
查看>>
去github里面找找也没有别人无人机+SLAM的工程
查看>>
现在明白为什么无名博客里好几篇文章在讲传感器的滞后
查看>>