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

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
量子位
小众软件
小众软件
C
Cybersecurity and Infrastructure Security Agency CISA
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tenable Blog
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
T
Threat Research - Cisco Blogs
Latest news
Latest news
Spread Privacy
Spread Privacy
C
Cyber Attacks, Cyber Crime and Cyber Security
NISL@THU
NISL@THU
T
Tor Project blog
Hacker News: Ask HN
Hacker News: Ask HN
V2EX - 技术
V2EX - 技术
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
K
Kaspersky official blog
Cyberwarzone
Cyberwarzone
博客园 - 叶小钗
博客园 - 聂微东
Last Week in AI
Last Week in AI
爱范儿
爱范儿
腾讯CDC
博客园 - Franky
美团技术团队
J
Java Code Geeks
O
OpenAI News
L
Lohrmann on Cybersecurity
Simon Willison's Weblog
Simon Willison's Weblog
有赞技术团队
有赞技术团队
T
Threatpost
G
GRAHAM CLULEY
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
I
Intezer
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
S
Security @ Cisco Blogs
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
Stack Overflow Blog
Stack Overflow Blog
Scott Helme
Scott Helme
H
Hacker News: Front Page
Cloudbric
Cloudbric

博客园 - 遗忘海岸

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