task.run多线程的坑

####问题
1.task.run默认会有线程池启用,但因为受ThreadPoolTaskScheduler的默认配置,启用多个子线程时,会1秒开启一个,如果想要减少整体运行时间并发多个线程,这其实达不到想要的效果

解决方案:

1.如果子方法有提供aync的方法,使用它

obj.getMethonAysnc();

2.指定CreateOptions为LongRunning(这样便会直接开一个新线程,而不是使用线程池)

//使用Task.Factory.StartNew比较方便的地方是方便设置TaskCreationOptions参数;
 Task.Factory.StartNew(()=> {
                Console.WriteLine("");
                Thread.Sleep(100);
            },TaskCreationOptions.LongRunning);
//new Task的使用方式,可以看到需要new一个task新类
  var task = new Task(() => { 
                Console.WriteLine("");
                Thread.Sleep(100);
            }, TaskCreationOptions.LongRunning);
 task.Start();

3.显示指定线程池的最小值和最大值

            ThreadPool.SetMinThreads(90, 100);
            ThreadPool.SetMaxThreads(90, 100);

            for (int i = 0; i < 20; i++)
            {
                // Task.Factory.StartNew();
                Task.Run(() =>
                {
                    Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.sss"));
                    Thread.Sleep(5000);
                });
            }

4.如果确实是各子线程无相关性,使用Parallel并发类

 //最简单的使用,Parallel.Invoke(...);
  public void Run1()
        {
            Thread.Sleep(2000);
            Console.WriteLine("Task 1 is cost 2 sec");
        }
        public void Run2()
        {
            Thread.Sleep(3000);
            Console.WriteLine("Task 2 is cost 3 sec");
        }

        private Stopwatch stopWatch = new Stopwatch();
        public void ParallelInvokeMethod()
        {
            stopWatch.Start();
            Parallel.Invoke(Run1, Run2);
            stopWatch.Stop();
            Console.WriteLine("Parallel run " + stopWatch.ElapsedMilliseconds + " ms.");

            stopWatch.Restart();
            Run1();
            Run2();
            stopWatch.Stop();
            Console.WriteLine("Normal run " + stopWatch.ElapsedMilliseconds + " ms.");
        }

参考:
https://www.cnblogs.com/springsnow/p/9409205.html
https://www.cnblogs.com/walterlv/p/10236390.html
https://www.cnblogs.com/yunfeifei/p/3993401.html

发布者

zebulun

我还是我,不一样的花火!