using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading; namespace Thread_synchronization_problem
{
class Program
{
static void Main(string[] args)
{
int x = ;
const int iterationNumber = ;//迭代次数
Stopwatch sw = Stopwatch.StartNew();//初始化sw
for (int i = ; i < iterationNumber; i++)
{
x++;
}
Console.WriteLine("不使用锁的情况下花费时间:{0}ms",sw.ElapsedMilliseconds);
sw.Start();//重置时间
for (int i = ; i < iterationNumber; i++)
{
Interlocked.Increment(ref x);//带锁的递增
}
Console.WriteLine("使用锁的情况下花费时间:{0}ms", sw.ElapsedMilliseconds);
Console.ReadLine();
}
}
}

可以看出使用锁的情况花费的时间是不使用锁的几倍;

最新文章

  1. Freemark基本语法知识(转)
  2. motto11
  3. 【C#】使用C#将类序列化为XML
  4. Redis命令小细节
  5. Kakfa揭秘 Day1 Kafka原理内幕
  6. C# 线程知识--使用ThreadPool执行异步操作
  7. 设置appicon和启动图
  8. bzoj 3287: Mato的刷屏计划 高精水题 &amp;&amp; bzoj AC150
  9. 打破C++ Const 的规则
  10. js常用内置对象及方法
  11. Spring MVC知识点整理
  12. Python练手例子(14)
  13. 移动端rem计算
  14. 【java工具】java常用工具
  15. node代理服务器
  16. nginx - 性能优化,突破十万并发
  17. ssh-copy-id 复制公钥到远程server
  18. 剖析Elasticsearch集群系列之一:Elasticsearch的存储模型和读写操作
  19. 1、编写第一个java程序--Hello—World
  20. 【开发者笔记】解析具有合并单元格的Excel

热门文章

  1. 迁移博客到Github Pages
  2. [转]aliyun阿里云Maven仓库地址——加速你的maven构建
  3. Could not find JSON in http://updates.jenkins-ci.org/update-center.json?id=default&amp;version=2.7.4
  4. RQNOJ 202 奥运火炬登珠峰:01背包
  5. JVM内存溢出环境备份方法
  6. BZOJ3123:[SDOI2013]森林
  7. shell 统计词频脚本
  8. PowerDesigner 导出 Excel
  9. CodeForces 1109F. Sasha and Algorithm of Silence&#39;s Sounds
  10. js学习笔记2:循环和try/catch/throw