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

推荐订阅源

N
Netflix TechBlog - Medium
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
博客园 - Franky
F
Fortinet All Blogs
D
Docker
博客园 - 司徒正美
腾讯CDC
Recent Announcements
Recent Announcements
The Cloudflare Blog
B
Blog RSS Feed
GbyAI
GbyAI
T
Tailwind CSS Blog
雷峰网
雷峰网
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
阮一峰的网络日志
阮一峰的网络日志

博客园 - 遗忘海岸

简协运动模拟 电磁场中的运动轨迹模拟 圆周运动模拟 带阻力的平抛运动 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个小时测试)