多线程测试

总结:

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.总次数;
        }

      
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值