zoukankan      html  css  js  c++  java
  • 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("王晶", "", 20, "市场部");
                Employee empTwo = new Employee("陈浩民", "", 24, "技术部");
                Employee empThree = new Employee("王诗玲", "", 25, "市场部");
                //Enqueue(T item) 将对象添加到结尾处
                currendQueue.Enqueue(empOne);
                currendQueue.Enqueue(empTwo);
                currendQueue.Enqueue(empThree);
    
                //获取包含的元素数
                if (currendQueue.Count > 0)
                {
                    //TryDequeue(out T result) 尝试移除并返回并发队列开头处的对象
                    currendQueue.TryDequeue(out Employee employee);
                    Console.WriteLine(employee);
    
                    if (currendQueue.Count > 0)
                    {
                        for (int i = 0; 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 + "]";
            }
        }
    }
  • 相关阅读:
    使用KNN算法手写体识别
    os内置模块
    python步长为负时的情况
    qplot()函数的详细用法
    python文件I/O
    python中 @property
    python中定制类
    python中多重继承与获取对象
    python继承,判断类型,多态
    python中访问限制
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/10642191.html
Copyright © 2011-2022 走看看