When should the task be considered "lengthy"?

When working with tasks, the rule, as a rule, is that the thread pool - commonly used, for example, calling Task.Run()or Parallel.Invoke()- should be used for relatively short operations. When working with lengthy work, we should use the flag TaskCreationOptions.LongRunningin order, as I understand it, to avoid clogging the thread pool queue, i.e. Push work on a newly created thread.

But what is long work? How long does it take in terms of time? Are there other factors besides the expected duration of the task when deciding whether to use LongRunning, for example, the expected processor architecture (frequency, number of cores, ...) or the number of tasks that will be undertaken immediately from the point of view of the programmer?

For example, suppose I have 500 tasks to process in a dedicated application, each of which takes 10-20 seconds. Should I start all 500 tasks using Task.Run (for example, in a loop), and then wait for all of them, perhaps like LongRunningleaving the default maximum concurrency level? Again, if I installed LongRunningin this case, would it not create 500 new threads and actually cause a lot of overhead and higher memory usage (due to the allocation of additional threads) compared to the absence LongRunning? This suggests that new tasks will not be scheduled for execution while the 500 are waiting.

I would suggest that the install decision LongRunningdepends on the number of requests made to the thread pool for a given period of time, and that LongRunningit should only be used for tasks that are expected to be significantly longer than most tasks associated with the thread pool, by definition, no more than a small percentage of all tasks. In other words, this is apparently a problem of optimizing the use of the pool and pool thread, which is likely to be solved in each case by testing, if at all. Am I right?

+4
source share
4 answers

. , , , . -, . ... , CPU. " ", CPU .

, , CPU. , , : " ", .

API, Task.Run LongRunning. - , LongRunning . , " ". , , , LongRunning , -. , ... . : D

, , API File.Exists. , , (, ), Task.Run - , LongRunning.

, , 100% , , - LongRunning.

, , LongRunning , "" " " , - , , ". , 99% - - . , , , API. - , - SynchronizationContext, .

, ? , , . , Parallel.For - , - , , , . ... , LongRunning, . , , await Task.WhenAll(...) .

+5

TaskCreationOptions.LongRunning . Parallel.For.

LongRunning , , . , .

, .

+2

, , , - , , Task.Run() Parallel.Invoke() - . TaskCreationOptions.LongRunning true, , , , .

LongRunning , "" 2 .

LongRunning , StartNew API.

, , -, , , , , . ?

. LongRunning . - , LongRunning.

+2

, TaskCreationOptions.LongRunning

so that ThreadPool continues to process work items, even if one task runs for a long period of time.

How to use it:

This is not a specific length as such ... Usually you use LongRunning only if you find that performance testing is not used, which leads to long delays in processing other work.

Source

+1
source

All Articles