C#-----线程安全的ConcurrentQueue<T>队列
ConcurrentQueue<T>队列是一个高效的线程安全的队列,是.Net Framework 4.0,System.Collections.Concurrent命名空间下的一个数据结构
- IsEmpty 获取一个值,判断是否为空
- Count 获取包含的元素数
- Enqueue(T item) 将对象添加到队列的结尾处
- TryDequeue(out T result) 尝试移除并返回并发队列开头处的对象
- TryPeek(out T result) 尝试返回开头处的对象但不将其移除
- ElementAt(int index) 返回序列中的指定索引处的元素
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace TestConcurrentQueue
{
class Program
{
static void Main(string[] args)
{
//ConcurrentQueue 表示线程安全的先进先出 (FIFO) 集合
ConcurrentQueue<Employee> currendQueue = new ConcurrentQueue<Employee>();
Employee empOne = new Employee("王晶", "男", , "市场部");
Employee empTwo = new Employee("陈浩民", "男", , "技术部");
Employee empThree = new Employee("王诗玲", "女", , "市场部");
//Enqueue(T item) 将对象添加到结尾处
currendQueue.Enqueue(empOne);
currendQueue.Enqueue(empTwo);
currendQueue.Enqueue(empThree); //获取包含的元素数
if (currendQueue.Count > )
{
//TryDequeue(out T result) 尝试移除并返回并发队列开头处的对象
currendQueue.TryDequeue(out Employee employee);
Console.WriteLine(employee); if (currendQueue.Count > )
{
for (int i = ; i < currendQueue.Count; i++)
{
Employee emp = currendQueue.ElementAt(i);
Console.WriteLine(emp);
}
}
}
}
} /// <summary>
/// 雇员类
/// </summary>
class Employee
{
/// <summary>
/// 雇员姓名
/// </summary>
public string EmpName { get; set; }
/// <summary>
/// 雇员性别
/// </summary>
public string EmpSex { get; set; }
/// <summary>
/// 雇员年龄
/// </summary>
public int EmpAge { get; set; }
/// <summary>
/// 雇员部门
/// </summary>
public string DeptName { get; set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="empName"></param>
/// <param name="empSex"></param>
/// <param name="empAge"></param>
/// <param name="deptName"></param>
public Employee(string empName, string empSex, int empAge, string deptName)
{
EmpName = empName;
EmpSex = empSex;
EmpAge = empAge;
DeptName = deptName;
} public override string ToString()
{
return "Employee[EmpName=" + EmpName + ",EmpSex=" + EmpSex + ",EmpAge=" + EmpAge + ",DeptName=" + DeptName + "]";
}
}
}
C#-----线程安全的ConcurrentQueue<T>队列的更多相关文章
- 线程安全的ConcurrentQueue<T>队列
队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队. ConcurrentQueue< ...
- 线程安全之ConcurrentQueue<T>队列
最近在弄一个小项目,大概600w行的数据,要进行数据清洗,因数据量偏大,如果单线程去执行,会造成效率偏低,只能用多线程了,但采用多线程存在线程安全问题,于是查了下资料,发现有ConcurrentQue ...
- 线程池ThreadPoolExecutor与阻塞队列BlockingQueue应用
1.线程池介绍 JDK5.0以上: java.util.concurrent.ThreadPoolExecutor 构造函数签名: public ThreadPoolExecutor( int co ...
- 转:JAVA线程池ThreadPoolExecutor与阻塞队列BlockingQueue
从Java5开始,Java提供了自己的线程池.每次只执行指定数量的线程,java.util.concurrent.ThreadPoolExecutor 就是这样的线程池.以下是我的学习过程. 首先是构 ...
- java多线程:线程池原理、阻塞队列
一.线程池定义和使用 jdk 1.5 之后就引入了线程池. 1.1 定义 从上面的空间切换看得出来,线程是稀缺资源,它的创建与销毁是一个相对偏重且耗资源的操作,而Java线程依赖于内核线程,创建线程需 ...
- 线程高级应用-心得7-java5线程并发库中阻塞队列Condition的应用及案例分析
1.阻塞队列知识点 阻塞队列重要的有以下几个方法,具体用法可以参考帮助文档:区别说的很清楚,第一个种方法不阻塞直接抛异常:第二种方法是boolean型的,阻塞返回flase:第三种方法直接阻塞. 2. ...
- spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue
一: ThreadPoolTaskExecutor是一个spring的线程池技术,查看代码可以看到这样一个字段: private ThreadPoolExecutor threadPoolExecut ...
- java线程(7)——阻塞队列BlockingQueue
回顾: 阻塞队列,英文名叫BlockingQueue.首先他是一种队列,联系之前Java基础--集合中介绍的Queue与Collection,我们就很容易开始今天的阻塞队列的学习了.来看一下他们的接口 ...
- Elasticsearch源码分析—线程池(十一) ——就是从队列里处理请求
Elasticsearch源码分析—线程池(十一) 转自:https://www.felayman.com/articles/2017/11/10/1510291570687.html 线程池 每个节 ...
随机推荐
- Arrange the Bulls [POJ2441] [状压DP]
题意 n头牛,m个房间,每头牛有自己喜欢的房间,问每头牛都住进自己喜欢的房间有多少种分配方法? Input In the first line of input contains two intege ...
- 如何使用JavaScript实现纯前端读取和导出excel文件
js-xlsx 介绍 由SheetJS出品的js-xlsx是一款非常方便的只需要纯JS即可读取和导出excel的工具库,功能强大,支持格式众多,支持xls.xlsx.ods(一种OpenOffice专 ...
- Gradle入门与使用
注:此篇博客主要是看官网的学习笔记:https://docs.gradle.org/current/userguide/installation.html 一.安装: 1.Gradle有内置的groo ...
- Node.js_ express.Router 路由器_模块化管理路由
路由器 express.Router 路由器 模块化管理 路由 基本使用: 路由模块 1. 引入 express const express = require('express'); 其他相关模块 ...
- php 常用正则
1 手机 /^1[34578]\d{9}$/2 qq /^[1-9][0-9]{5,10}$/3 邮箱 /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za ...
- mysql having和where的区别
having子句与where子句一样,都是用于条件判断的. 区别1 where是判断数据从磁盘读入内存的时候 having是判断分组统计之前的所有条件 区别原理 区别2 having子句中可以使用字段 ...
- CentOS7搭建SVN服务器
首先,你得有个VPS,我用的是搬瓦工. 安装步骤如下:1.yum install subversion2.查看安装版本 svnserve --version 3.创建SVN版本库目录 mkdir ...
- vins-mono中的imu参数设置
na:加速度计的测量噪声 nw:陀螺仪的测量噪声 nba: randow walk noise随机游走噪声 nbw:randow walk noise随机游走噪声 ba:加速度计的偏差 bw:陀螺仪的 ...
- [daily] cscope
手册: http://cscope.sourceforge.net/cscope_vim_tutorial.html 下载 cscope_maps.vim 到 $HOME/.vim/plugin/ 目 ...
- RMAN备份filesperset用法
用filesperset控制备份集的尺寸 当指定filesperset参数时,rman比较filesperset与自动计算出来的值(对每个已分配通道的文件数目) 并取其中较小的那个值来保证所有的通道被 ...