惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
月光博客
月光博客
博客园_首页
博客园 - 叶小钗
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
量子位
小众软件
小众软件
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
IT之家
IT之家
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 遗忘海岸

简协运动模拟 电磁场中的运动轨迹模拟 圆周运动模拟 带阻力的平抛运动 devexpress gridview master,detail视图 focuseRowHandle 同步选中 C# Tcp Server端实现,使用TcpListener 深信服务超融合管理Api调用 GDI+画工作流图的一些总结 绘制贝塞而曲线 GDI+画直线带箭头 devexpress schedulerControl Gantt View 使用 逻辑回归损失函数求导 matplotlim柱状图 匀加速运动模拟python,(matplotlib) C# 实现排列 python 类鸟群Boids C#动态编译 正太分布数据排序后分段数据的方差与标准差 Android 的一个通用列表单选AlertDialog封装
C# 队列的一些并发模拟
遗忘海岸 · 2024-11-08 · via 博客园 - 遗忘海岸

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections.Concurrent;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Queue<String> _Queue = new Queue<String>(100000);
        private ConcurrentQueue<String> _Queue2 = new ConcurrentQueue<String>();
        private bool _Enable = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            _Enable = true;
            ThreadPool.QueueUserWorkItem(o => { WriteQueue2(); }, null);
            ThreadPool.QueueUserWorkItem(o => { WriteQueue2(); }, null);
            ThreadPool.QueueUserWorkItem(o => { WriteQueue2(); }, null);
            ThreadPool.QueueUserWorkItem(o => { ReadQueue2(); }, null);
            ThreadPool.QueueUserWorkItem(o => { ReadQueue2(); }, null);
        }
        private void ReadQueue()
        {
            while (_Enable)
            {
                try
                {
                    Console.WriteLine("Find...");
                    var guid = Guid.NewGuid().ToString();
                    var fIt = _Queue.FirstOrDefault(ent => ent == guid);
                    if (fIt != null)
                    {
                        Console.WriteLine("Shit!");
                    }
                    //Thread.Sleep(10);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("ReadQueue:" + ex.Message);

                }
            }
        }
        private void WriteQueue()
        {
            var rnd = new Random();
            while (_Enable)
            {
                var r = rnd.Next(0, 100);
                if (r > 50)
                {
                    for (int i = 0; i < 50; i++)
                    {
                        _Queue.Enqueue(Guid.NewGuid().ToString());
                    }
                }
                else
                {
                    if (_Queue.Count >= 200)
                    {
                        for (int i = 0; i < 100; i++)
                        {
                            _Queue.Dequeue();
                        }
                    }
                }
                Console.WriteLine("Queue.Count:" + _Queue.Count);
                //Thread.Sleep(10);
            }
        }

        private void ReadQueue2()
        {
            while (_Enable)
            {
                try
                {
                    Console.WriteLine("Find...");
                    var guid = Guid.NewGuid().ToString();
                    var fIt = _Queue2.FirstOrDefault(ent => ent == guid);
                    if (fIt != null)
                    {
                        Console.WriteLine("Shit!");
                    }
                    //Thread.Sleep(10);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("ReadQueue:" + ex.Message);

                }
            }
        }
        private void WriteQueue2()
        {
            var rnd = new Random();
            while (_Enable)
            {
                var r = rnd.Next(0, 100);
                if (r > 50)
                {
                    for (int i = 0; i < 50; i++)
                    {
                        _Queue2.Enqueue(Guid.NewGuid().ToString());
                    }
                }
                else
                {
                    if (_Queue2.Count >= 200)
                    {
                        for (int i = 0; i < 100; i++)
                        {
                            string outIt=null;
                            _Queue2.TryDequeue(out outIt);
                        }
                    }
                }
                Console.WriteLine("Queue.Count:" + _Queue2.Count);
                //Thread.Sleep(10);
            }
        }

        private void NewMethod()
        {
            var frm = new Form2();
            frm.Text = Guid.NewGuid().ToString();
            frm.Show();

            MessageBox.Show(this, "xxx", "x", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            Console.WriteLine("xxx");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var pinfo = new PInfo();
            var s = pinfo.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            _Enable = false;
        }
    }

}

View Code

采用ConcurrentQueue后未发现 FirstOrDefault方法时报错 (4个小时测试)