多线程测试

总结:

thread能启动非常多的线程同时工作,但threadpool和Task同时并行的线程数有限,约20个左右,但Threadpool设置最大并行数后则不会超过此数。

另:thread和threadpool的子线程中如果有未处理的异常,则主程序则会出现“*** 已停止工作”这样的错误(程序自动退出),而Task的子线程中出现未处理的异常时不影响主程序,即主程序不会退出。

 

 public partial class Form1 : Form
    {
        int count = 0;
        int 总次数 = 0;
        Random r = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //有300多个线程并行
            this.count = 0;
            this.总次数 = 0;
            for (int m = 0; m < 400; m++)
            {
                System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(this.run));
                t.Start();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //有10到40个线程并行
            this.count = 0;
            this.总次数 = 0;
            int a,b; //得到32767和1000
             System.Threading.ThreadPool.GetMaxThreads(out a,out b);
            for (int m = 0; m < 400; m++)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback( this.run),0);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //有10到40个线程并行
            this.count = 0;
            this.总次数 = 0;
            int a, b; //得到32767和1000
            System.Threading.ThreadPool.GetMaxThreads(out a, out b);
            //设置为最大10后,并行数稳定在10个,设置最大100后,最多并行还是10到40个,这应该和机器有关
            System.Threading.ThreadPool.SetMaxThreads(10, 100);  
            for (int m = 0; m < 400; m++)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(this.run), 0);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //有10到40多个线程并行,运行效率基本同于QueueUserWorkItem
            this.count = 0;
            this.总次数 = 0;
           
            for (int m = 0; m < 400; m++)
            {
                System.Threading.Tasks.Task.Factory.StartNew(this.run);
            }
        }

        private void run()
        {
            Interlocked.Increment(ref this.总次数);
            Interlocked.Increment(ref this.count);
            System.Threading.Thread.Sleep(r.Next(10) * 1000);

            System.Threading.Interlocked.Decrement(ref this.count);

            this.异常();
        }

        private void run(object ob)
        {
            Interlocked.Increment(ref this.总次数);
            Interlocked.Increment(ref this.count);
         
            System.Threading.Thread.Sleep(r.Next(10) * 1000);

            System.Threading.Interlocked.Decrement(ref this.count);

            this.异常();
        }

        private void 异常()
        {
            throw new Exception("ddd");

            //try
            //{
            //    throw new Exception("ddd");
            //}
            //catch (Exception ex)
            //{

            //}
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = this.count.ToString() + " / " + this.总次数;
        }

      
    }

 

另外,多线程方式使用  System.Threading.Thread 创建线程时非常慢,创建超过200个创建就会非常明显的阻塞,如:

  private void button4_Click(object sender, EventArgs e)
        {
            for (int m = 0; m < 400; m++)
            {
                System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(this.run));
                t.Start();
            }
        }  //这个是在主线程中创建多个子线程,但界面会明显失去响应。

 

解决方法:使用async函数,在主界面中创建10000路异步子线程也不会卡顿

 private void button1_Click(object sender, EventArgs e)
        {
            for (int m = 0; m < 1000; m++)
            {
                this.Ping("3");
            }
        }

        async private void Ping(string host)
        {
         
            System.Threading.Interlocked.Increment(ref this.count);
            Random r = new Random();
            await Task.Delay(r.Next( 5000));
            System.Threading.Interlocked.Increment(ref this.count2);
        }

 

posted @   81  阅读(439)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示