遗忘海岸

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

导航

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 2024-11-08 08:25  遗忘海岸  阅读(4)  评论(0编辑  收藏  举报