当前位置: 首页 > news >正文

.NET 5种线程安全集合

在.NET中,有许多种线程安全的集合类,下面介绍五种我们常用的线程安全集合以及他们的基本用法。

ConcurrentBag

ConcurrentBag 是一个线程安全的无序包。它适用于在多线程环境中频繁添加和移除元素的情况。

ConcurrentBag<int> concurrentBag = new ConcurrentBag<int>();// 添加元素
concurrentBag.Add(1);// 尝试添加元素
concurrentBag.TryAdd(2);// 移除元素
int item;
concurrentBag.TryTake(out item);

ConcurrentQueue

ConcurrentQueue 是一个线程安全的无序队列。它适用于在多线程环境中频繁添加元素和移除元素(通常是先进先出方式)的情况。

ConcurrentQueue<int> concurrentQueue = new ConcurrentQueue<int>();// 添加元素
concurrentQueue.Enqueue(1);// 尝试添加元素
bool isSuccess = concurrentQueue.TryEnqueue(2);// 移除元素
int item;
bool isRemoved = concurrentQueue.TryDequeue(out item);

ConcurrentStack

ConcurrentStack 是一个线程安全的堆栈。它适用于在多线程环境中频繁添加元素和移除元素(通常是后进先出方式)的情况。

ConcurrentStack<int> concurrentStack = new ConcurrentStack<int>();// 添加元素
concurrentStack.Push(1);// 尝试添加元素
bool isSuccess = concurrentStack.TryPush(2);// 移除元素
int item;
bool isRemoved = concurrentStack.TryPop(out item);

ConcurrentDictionary<TKey, TValue>

ConcurrentDictionary<TKey, TValue> 是一个线程安全的字典。它适用于在多线程环境中频繁添加、移除和查找键值对的情况。

ConcurrentDictionary<int, string> concurrentDictionary = new ConcurrentDictionary<int, string>();// 添加或更新键值对
concurrentDictionary.AddOrUpdate(1, "One", (key, oldValue) => "NewOne");// 尝试添加或更新键值对
bool isSuccess = concurrentDictionary.TryAdd(2, "Two");
bool isUpdated = concurrentDictionary.TryUpdate(2, "NewTwo", "Two");// 移除键值对
string removedValue;
bool isRemoved = concurrentDictionary.TryRemove(1, out removedValue);// 获取值
string value;
bool isFound = concurrentDictionary.TryGetValue(1, out value);

BlockingCollection

BlockingCollection 是线程安全的集合,提供了可阻塞的添加和移除方法

BlockingCollection<int> blockingCollection = new BlockingCollection<int>();// 添加元素,如果集合已满,则阻塞当前线程
blockingCollection.Add(1);// 移除元素,如果集合为空,则阻塞当前线程
int item = blockingCollection.Take();

 

 

相关文章:

  • 数学专题3 - 质因数分解
  • 凡人修仙传8w字大纲pdf
  • 线程池详解并使用Go语言实现 Pool
  • python docx 添加动态表格
  • 从汇编看函数调用
  • 008 CSS盒子模型
  • 如何成为一名嵌入式C语言高手?
  • 突破编程_前端_SVG(概述)
  • 通俗易懂的理解 ADC(2)
  • zabbix绑定钉钉进行通知,网页端添加JavaScript,无脑式操作
  • sharo反序列化漏洞
  • 算法| ss 双指针
  • CentOS7安装Tomcat
  • 如何在plesk面板安装域名付费SSL证书
  • 云原生架构(微服务、容器云、DevOps、不可变基础设施、声明式API、Serverless、Service Mesh)
  • -------------------- 第二讲-------- 第一节------在此给出链表的基本操作
  • @angular/forms 源码解析之双向绑定
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • 10个确保微服务与容器安全的最佳实践
  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • Laravel Telescope:优雅的应用调试工具
  • laravel5.5 视图共享数据
  • Linux gpio口使用方法
  • MD5加密原理解析及OC版原理实现
  • Promise初体验
  • ReactNative开发常用的三方模块
  • Sequelize 中文文档 v4 - Getting started - 入门
  • session共享问题解决方案
  • spring security oauth2 password授权模式
  • supervisor 永不挂掉的进程 安装以及使用
  • Vue.js 移动端适配之 vw 解决方案
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 笨办法学C 练习34:动态数组
  • 解析 Webpack中import、require、按需加载的执行过程
  • 快速构建spring-cloud+sleuth+rabbit+ zipkin+es+kibana+grafana日志跟踪平台
  • 关于Android全面屏虚拟导航栏的适配总结
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • $.ajax中的eval及dataType
  • (01)ORB-SLAM2源码无死角解析-(56) 闭环线程→计算Sim3:理论推导(1)求解s,t
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (附源码)springboot社区居家养老互助服务管理平台 毕业设计 062027
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (紀錄)[ASP.NET MVC][jQuery]-2 純手工打造屬於自己的 jQuery GridView (含完整程式碼下載)...
  • (切换多语言)vantUI+vue-i18n进行国际化配置及新增没有的语言包
  • (原)Matlab的svmtrain和svmclassify
  • (转)iOS字体
  • (转)人的集合论——移山之道
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • .mysql secret在哪_MySQL如何使用索引
  • .Net MVC4 上传大文件,并保存表单
  • .NET Standard 的管理策略
  • .net 前台table如何加一列下拉框_如何用Word编辑参考文献
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题
  • .Net环境下的缓存技术介绍