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 线程池 每个节 ...
随机推荐
- HDU 2008 数值统计
题目链接:HDU 2008 Description 统计给定的n个数中,负数.零和正数的个数. Input 输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的 ...
- Json.NET Performance Tips
原文: http://www.newtonsoft.com/json/help/html/Performance.htm To keep an application consistently fas ...
- xmoj142
https://code.mi.com/problem/list/view?id=142 暴力. #include <bits/stdc++.h> using namespace std; ...
- react路由
针对多个列表导航公用一个组建,然后 有两种路由方式 1.import {HashRouter as Router,Route,Link} from 'react-router-dom' 不过这个路由中 ...
- <c:forEach var="role" items="[entity.Role@d54d4d, entity.Role@1c61868, entity.Role@6c58db, entity.Role@13da8a5]"> list 集合数据转换异常
<c:forEach var="role" items="[entity.Role@d54d4d, entity.Role@1c61868, entity.Role ...
- CSS---光标cursor设置、浮动布局与clear的关系
光标设置 {cursor:auto;}--光标根据需要自动变化. {cursor:crosshair;}--光标变成“+”. {cursor:pointer;}--光标变成手指模式. {cursor: ...
- Mac ssh启动和停止
原文地址:http://blog.csdn.net/cwj649956781/article/details/37913637 mac本身安装了ssh服务,默认情况下不会开机自启 1.启动sshd服务 ...
- delphi 中实现当期日期 减去 若干小时的方法
假定当期日期为:2011-08-01 15:00:00 now - 1 :代表前一天的日期 返回值:2011-07-31 15:00:00 now - 1/3 :代表8小时前 ...
- 如何配置Bitbucket的ssh
在bitbucket上使用https协议,经常会在提交代码的时候出错,让人很着急上火,但是用ssh就要方便很多.下面介绍一下设置ssh的方法:1.在终端中运行ssh-keygen.2.然后一路ente ...
- get请求02
import requests r = requests.get("http://www.baidu.com") print(r.status_code) #状态码 print(r ...