Home > Net >  A similar thread pool that Task? Keep some Task method at the end of the execution thread, rather th
A similar thread pool that Task? Keep some Task method at the end of the execution thread, rather th

Time:04-26

Parallel operation over a few days ago, in a great god's help, such as wanghui0380 messing about, but was later found to use task to multi-threaded parallel processing after some things, if there is no call for a period of time (a few seconds to tens of seconds) this task method called again after practice the speed will slow a lot,,, continuous execution would not have this problem, the observed is the task of parallel processing thread automatic start/exit,

Wanted to try can solve through the thread pool, but because the program structure is done with a task to do now, so want to change under the circumstances of less program, the task can finish the similar function? Make some threads preserved?
The original code about the following:
 
Public static Task TestTask (int Index)
{
Task task1=Task. Run (()=& gt;
{
//high time-consuming process,,
return;
});
Return task1.
}


 

The Parallel. For (0, 6, new ParallelOptions () {MaxDegreeOfParallelism=6}, async (I)=& gt;
{
Await TestTask (I);
});

Console. WriteLine ($" end ");



Another problem with the Console. WriteLine ($" end "); Always be carried out early, not wait until the Parallel For processing is complete, as above with the async (I) also is such? Can you tell me how should solve?

CodePudding user response:

How didn't see, has been the use of the Parallel, also need a Task,,
 
Public static voidTestTask (int Index)
{
//high time-consuming process,,
}

 
The Parallel. For (0, 6, new ParallelOptions () {MaxDegreeOfParallelism=6}, (I)=& gt;
{
TestTask (I);
});

Console. WriteLine ($" end ");

CodePudding user response:

To solve a problem after you

 ConcurrentBag Bag=new ConcurrentBag (a);//note here please use the thread security class list, because you have the following parallel the actual will be a multithreaded, if use the list that is not thread safe list, can produce accident 
The Parallel. For (0, 6, new ParallelOptions () {MaxDegreeOfParallelism=6}, (I)=& gt;
{
Bag. The add (TestTask (I));
});

Await Task. WhenAll (bag)

CodePudding user response:

Look at this is very intuitive, must have a thread pool, at the bottom of the

CodePudding user response:

As for your first question, actually we can't solve,

Because you are still caught in a myth, you think of the thread will be executed immediately, very persistent pursuit of the "parallel processing Task thread automatic start/exit,"

Actually is not at all, before we told you, thread at all there's no immediate execution, the thread has its own state: create, ready, execution, hang, death

System has a scheduler to thread scheduling threads, so didn't you say what start, automatic exit how slow cause you

So what problem I can't continue to illustrate this, we'll just have to give you conclusion
1. The task itself is a thread pool, so you don't need to entanglements what thread pool, he can solve the problem of creating, namely he will die from the pond to pick a nearby state thread to you, and then set to the ready state, which is compared with you create a thread, the thread pool is the purpose of "the number of constraints to create, save the creation process, direct management in place, execution, hangs, death"
2. No matter the task, the thread, all the scheduler thread scheduling, thread scheduler has several strategies

Public enum TaskCreationOptions
{
None=0,
PreferFairness=1,
LongRunning=2,
AttachedToParent=4,
DenyChildAttach=8,
HideScheduler=16
}

This you see (of course the traditional thread priority, just in the task system, the priority is the enumeration of default implementation above, such as longruning priority is low priority)


3. Don't tangle with thread thread, you need to look at the current general parallel threads, not about what thread, o death, is born,

If the system current parallel thread 1 w a, you now new thread 1 1 w is zero, you feel now scheduler when to switch from the time slice scheduling (we'll hit you have 8 cores, you can run at the same time 8 kernel thread) how much risk in the moment you are ready, is scheduled to one of the eight nuclear, obviously, the more you parallel threads, he immediately run the smaller chance of

So, please call in front of us, also said the number of concurrent control, on the basis of the guarantee business at the same time, please try to reduce a single thread of execution time make little task ()
Is clear: if the previous 1 w are small tasks, go quickly, such as 8000 four time cycle slip away, then you are qualifying for 2001, you will have a greater chance to be implemented immediately
Is obvious: if I also control the number of concurrent, the subsequent add threads will be less, I now this will have a greater chance to immediately

CodePudding user response:

From the use of the pool, the same will also ask you to try to split into small tasks and control the number of concurrent

Pool itself has a number, so if you create infinite big task, as a result, the thread pool, full (or abnormal, or waiting)

If there is a release is waiting pool, of course he will slowly

So you may as well take 3 words, their processing, don't like those blogs garden of nouns can't write code, we don't nouns, whether the thread, or a thread pool, or other technical
No matter what you use nouns to write code, article 3
1. Have the IO cleared up memory
2. To calculate, as far as possible into small tasks
3. Under the premise of without affecting the design requirements and constraints perform quantity

CodePudding user response:

We, the strategy of task one

PreferFairness: prompt TaskScheduler in a fair way to arrange task as much as possible, which means that the earlier arrangement task will be more likely to run early, and later arrange the running task will be more likely to run late,

Fair as far as possible -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - just as fair, is not absolutely fair
This means that the earlier arrange task will be more likely to run early -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- is more may be ahead of schedule, the earlier is possible, as far as possible be ahead of schedule
And later arrange the running task will be more likely to run late, -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the same tone, his big probability will be delay the schedule, but only that the big probability to delay

CodePudding user response:

The Task is to package the thread pool, please don't to pick bones in the eggs

Async/await structure is an asynchronous callback code synchronization method and syntactic sugar
  •  Tags:  
  • C#
  • Related