首页 > 代码库 > 多线程下操作数字要用类System.Threading.Interlocked
多线程下操作数字要用类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); }
多线程下操作数字要用类System.Threading.Interlocked
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。