Why do I have a task that lasts 10 seconds

I am currently studying TPL and have come up with the first test, which no longer gives the expected result. Is my code wrong or is this the expected result I was puzzled by the fact that I start 20 of those that do nothing but wait, and I expected the whole process to complete in 5 seconds, and it will take much more, in addition, I have a task that now takes 10 s instead of the expected 5.

Thanks for the help.

using System; using System.Collections.Generic; using System.Threading.Tasks; namespace ConsoleApplication3Task { class Program { static void Main(string[] args) { var tasks = new List<Task<string>>(); for (var i = 0; i < 20; i++) { var task = Task.Factory.StartNew<string>((index) => { var start = DateTime.UtcNow; Task.Delay(TimeSpan.FromSeconds(5)).Wait(); var end = DateTime.UtcNow; return string.Format("start={0}, duration={1} for task={2}", start.TimeOfDay, (end - start).TotalSeconds, index); }, i); tasks.Add(task); } Task.WaitAll(tasks.ToArray()); tasks.ForEach((t) => Console.WriteLine(t.Result)); } } } 

and output:

 start=10:07:19.8499784, duration=9,4992059 for task=0 start=10:07:19.8489785, duration=9,5002058 for task=1 start=10:07:19.8499784, duration=9,4992059 for task=2 start=10:07:19.8499784, duration=9,4992059 for task=3 start=10:07:19.8499784, duration=9,4992059 for task=4 start=10:07:19.8489785, duration=9,5002058 for task=5 start=10:07:19.8489785, duration=9,5002058 for task=6 start=10:07:19.8489785, duration=9,5002058 for task=7 start=10:07:20.8481051, duration=5,0016351 for task=8 start=10:07:21.8492322, duration=5,5006984 for task=9 start=10:07:22.8483595, duration=5,5046991 for task=10 start=10:07:23.8494862, duration=5,4996981 for task=11 start=10:07:24.8486135, duration=5,0006344 for task=12 start=10:07:25.8497402, duration=5,0116365 for task=13 start=10:07:25.8507412, duration=5,0106355 for task=14 start=10:07:26.8488675, duration=5,0096356 for task=15 start=10:07:27.3499306, duration=5,0076366 for task=16 start=10:07:27.3499306, duration=5,0076366 for task=17 start=10:07:28.3530586, duration=5,0126368 for task=18 start=10:07:28.3530586, duration=5,0126368 for task=19 Press any key to continue . . . 
+4
source share
1 answer

The reason this happens is because the TPL (in fact, the thread pool) throttles the number of threads that you can create right away. You should not modify this behavior since it allows you to immediately stop loading threads, which can cause problems.

However, you can increase the number of threads instantly available in the thread pool using ThreadPool.SetMinThreads() :

 ThreadPool.SetMinThreads(20, 20); 

You should almost always do this for test purposes only.

If you try to add this line to the top of your program, it will make it behave as expected.

Also note that the actual delay comes from this line:

 Task.Delay(TimeSpan.FromSeconds(5)).Wait(); 

.Wait() calls the threadpool thread to start, and it throttles so that it does not start immediately.

You can verify this by changing this line to:

 Thread.Sleep(TimeSpan.FromSeconds(5)); 

Then you will see that the thread takes 5 seconds even without using ThreadPool.SetMinThreads(20, 20);

+3
source

Source: https://habr.com/ru/post/1485014/


All Articles