遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

C# 队列的一些并发模拟

复制代码
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个小时测试)

posted on   遗忘海岸  阅读(7)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2017-11-08 证明 U and V={0}时 dim(U+V)=dim(U)+dim(V)
2011-11-08 几个用于序列化的代码片段
点击右上角即可分享
微信分享提示