博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程测试
阅读量:7239 次
发布时间:2019-06-29

本文共 2174 字,大约阅读时间需要 7 分钟。

总结:

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

转载地址:http://wugfm.baihongyu.com/

你可能感兴趣的文章
具体数学第二版第四章习题(2)
查看>>
DotNetBar.7.0 Crack
查看>>
D3D中深度测试和Alpha混合的关系
查看>>
延时执行和取消延时执行
查看>>
关于线程安全
查看>>
使用Java自带的VisualVM监控远程主机JVM内存使用情况
查看>>
123——Appium Girls活动
查看>>
Linux系统CPU频率调整工具使用
查看>>
使用大于16TB的ext4文件系统
查看>>
jquery ajax cache的问题
查看>>
VIM 与 系统剪切版
查看>>
我用iPad / iTouch来做什么
查看>>
php的mysql_insert_id()返回值问题
查看>>
css属性兼容
查看>>
Hadoop源码分析之心跳机制
查看>>
第三章初步了解函数
查看>>
[转] PHP常见的两个面试题
查看>>
asp.net MVC3 View视图
查看>>
利用Nginx搭建http和rtmp协议的流媒体服务器[转]
查看>>
面试笔试
查看>>