TH.L 2021-06-22 11:56 采纳率: 0%
浏览 361

C#多线程同时执行时耗时比单个线程执行耗时会增加

运行结果: 

 源码:


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private object obj;
        private void ceshiThread(Object obj)
        {
                Stopwatch sp = new Stopwatch();
                sp.Restart();
                int index = (int)obj;
                int count = 0;
                for (int i = 0; i < 10000; i++)
                {
                    for (int j = 0; j < 10000; j++)
                    {
                        int value = i * j;
                        count++;
                    }
                }
                sp.Stop();
                textBox1.AppendText(string.Format("线程 {0} 执行次数 {1} ,总耗时 {2}", index, count, sp.ElapsedMilliseconds.ToString()));
                textBox1.AppendText(Environment.NewLine);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //int a = 0;
            //while (a < 100)
            {
                for (int i = 0; i < 4; i++)
                {
                    Thread th = new Thread(ceshiThread);
                    th.IsBackground = true;
                    th.Start(i);
                    //Task t = new Task(() => ceshiThread(i));
                    //t.Start();
                }
                //a++;
                //Thread.Sleep(5);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Parallel.For(0, 4, (i) =>
            //{
            //    ceshiThread(i);
            //});

            for (int i = 0; i < 4; i++)
            {
                ThreadPool.QueueUserWorkItem(ceshiThread, i);
            }
            //Task t = new Task(() => ceshiThread(0));
            //t.Start();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //for (int i = 0; i < 100; i++)
            //{
                Thread th = new Thread(ceshiThread);
                th.IsBackground = true;
                th.Start(1);
                //Thread.Sleep(200);
            //}
            //Thread th = new Thread(ceshiThread);
            //th.IsBackground = true;
            //th.Start(1);
        }

    }
}
  • 写回答

3条回答 默认 最新

  • TH.L 2021-06-22 14:19
    关注

    新增委托后的源码: 

    private void ceshiThread(Object obj)
            {
                //用于时间测试
                Stopwatch sp = new Stopwatch();
                //开始测试时间
                sp.Restart();
                int index = (int)obj;
                int count = 0;
                for (int i = 0; i < 10000; i++)
                {
                   for (int j = 0; j < 10000; j++)
                        {
                            int value = i * j;
                            count++;
                        }
                 }
                sp.Stop();
                //线程委托
                this.Invoke(new dvg(delegate () {
                    textBox1.AppendText(string.Format("线程 {0} 执行次数 {1} ,总耗时 {2}", index, count, sp.ElapsedMilliseconds.ToString()));
                    textBox1.AppendText(Environment.NewLine);
                }));
            }

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器