C# 线程同步计数存在的问题
2024-09-04 12:04:16
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();
}
}
}
可以看出使用锁的情况花费的时间是不使用锁的几倍;
最新文章
- Freemark基本语法知识(转)
- motto11
- 【C#】使用C#将类序列化为XML
- Redis命令小细节
- Kakfa揭秘 Day1 Kafka原理内幕
- C# 线程知识--使用ThreadPool执行异步操作
- 设置appicon和启动图
- bzoj 3287: Mato的刷屏计划 高精水题 &;&; bzoj AC150
- 打破C++ Const 的规则
- js常用内置对象及方法
- Spring MVC知识点整理
- Python练手例子(14)
- 移动端rem计算
- 【java工具】java常用工具
- node代理服务器
- nginx - 性能优化,突破十万并发
- ssh-copy-id 复制公钥到远程server
- 剖析Elasticsearch集群系列之一:Elasticsearch的存储模型和读写操作
- 1、编写第一个java程序--Hello—World
- 【开发者笔记】解析具有合并单元格的Excel
热门文章
- 迁移博客到Github Pages
- [转]aliyun阿里云Maven仓库地址——加速你的maven构建
- Could not find JSON in http://updates.jenkins-ci.org/update-center.json?id=default&;version=2.7.4
- RQNOJ 202 奥运火炬登珠峰:01背包
- JVM内存溢出环境备份方法
- BZOJ3123:[SDOI2013]森林
- shell 统计词频脚本
- PowerDesigner 导出 Excel
- CodeForces 1109F. Sasha and Algorithm of Silence&#39;s Sounds
- js学习笔记2:循环和try/catch/throw