• 多线程下操作数字要用类System.Threading.Interlocked


    public class ServiceStatistics
    {
    private int _sourseCount = 0;
    /// <summary>
    /// 处理源数据量
    /// </summary>
    public int SourseCount
    {
    get { return _sourseCount; }
    }
    private int _destinationCount = 0;
    /// <summary>
    /// 返回结果数据量
    /// </summary>
    public int DestinationCount
    {
    get { return _destinationCount; }
    }
    
    private long _elapsedMilliseconds = 0L;
    /// <summary>
    /// 执行耗时
    /// </summary>
    public long ElapsedMilliseconds
    {
    get { return _elapsedMilliseconds; }
    }
    /// <summary>
    /// 增加源数据量
    /// </summary>
    /// <param name="count"></param>
    public void IncrementSourse(int count = 1)
    {
    if (count < 1)
    return;
    System.Threading.Interlocked.Add(ref _sourseCount, count);
    }
    /// <summary>
    /// 增加结果数据量
    /// </summary>
    /// <param name="count"></param>
    public void IncrementDestination(int count = 1)
    {
    if (count < 1)
    return;
    System.Threading.Interlocked.Add(ref _destinationCount, count);
    }
    /// <summary>
    /// 增加执行耗时
    /// </summary>
    /// <param name="milliseconds"></param>
    public void Elapsed(long milliseconds)
    {
    if (milliseconds < 1L)
    return;
    System.Threading.Interlocked.Add(ref _elapsedMilliseconds, milliseconds);
    }
  • 相关阅读:
    UVa 1252 20个问题
    HDU 2196 Computer
    HDU 1520 Anniversary party
    HDU 2066 一个人的旅行
    UVa 10048 噪音恐惧症(Floyd)
    UVa 247 电话圈(Floyd传递闭包)
    HDU 2544 最短路(Dijkstra)
    HDU 1548 A strange lift (Dijkstra)
    UVa 1151 买还是建
    UVa 1395 苗条的生成树(Kruskal+并查集)
  • 原文地址:https://www.cnblogs.com/gyt-xtt/p/6688324.html
Copyright © 2020-2023  润新知