多线程计数,怎么保持计数准确的方法
///创建一个Sigleton实例
System.Threading.Thread.Sleep(100);
int i = 0; while (MyCounter.GetCounter() < 200) { //保证计数与输出一致,即便计数与输出之间加上时间间隔也会为这块区域加锁,防止其他线程操作 lock (this) { ///开始计数 MyCounter.Add(); System.Threading.Thread.Sleep(100); Thread thread = Thread.CurrentThread; results += "线程"; results += i++.ToString() + "――〉" + thread.Name + " "; results += "当前的计数:"; results += MyCounter.GetCounter().ToString(); results += "\n";
Console.WriteLine(results);
// 清空显示字符串 results = ""; } }}
public void StartMain() {
Thread thread0 = Thread.CurrentThread; thread0.Name = "Thread 0"; Thread thread1 =new Thread(new ThreadStart(DoSomeWork)); thread1.Name = "Thread 1"; Thread thread2 =new Thread(new ThreadStart(DoSomeWork)); thread2.Name = "Thread 2"; Thread thread3 =new Thread(new ThreadStart(DoSomeWork)); thread3.Name = "Thread 3";
thread1.Start(); thread2.Start(); thread3.Start(); ///线程0也只执行和其他线程相同的工作 DoSomeWork(); } }
- 未将对象引用设置到对象的实例--可能出现的问题总结
- Java设计模式学习笔记(一)
- MATLAB 截取有目标物体算法
- MATLAB 截取有目标物体算法
- python not find in register
- NYOJ 2 括号配对问题
- 数组做函数参数传递和函数返回值
- 轮盘赌算法
- Android 异步任务
- [android]fmodex在某些android设备上声音延迟Latency
- 关于FlexPaper 2.1.2版本 二次开发 Logo 、打印、搜索、缩略图、添加按钮、js交互、右键菜单等相关问题
- android pcm编解码
- NYOJ 860 又见01背包
- Editplus快捷键
- Ogre着色方式和光照模型(shading mode vs lighting model)
- php __set()和__get()函数
- USACO 4.3 Street Race&The Primes(跪了)
- 页面跳转的几种方法
- ios 耳机线控
- POJ-3267 The Cow Lexicon 简单DP