문제

i am filling a task factory with tasks something like this (pseudocode):

private int _taskcounter;

snip

var factory = new TaskFactory();
for(var i = 0;i<1000;i++)
   factory.StartNew(() => doWork());

snip

private void doWork(){
   Interlocked.Increment(ref _taskcounter);
   //do some slow I/O work here
   Interlocked.Decrement(ref _taskcounter);
}

if you now watch the _taskcounter you can see
initially its starting about 10 tasks but then it starts 1 to 2 tasks/s until the queue is finished.

petty obvious behavior, isnt it?

my question is:
is there a way to force the task factory to start more tasks on the start?

im already limiting the maximum of running tasks with a variation of some msdn example class to prevent excessive memory consumption.
or is there something wrong with this funny measurement method or my consideration?

its not necessary to run all the work in different threads otherwise i wouldnt use the factory
i only want benefit from the non work stealing behavior of the factory.

for everything else im relying on the intelligence of the factory.

도움이 되었습니까?

해결책

As I know TaskFactory uses ThreadPool, so you can try SetMaxThreads method. But actually it is designed to optimize resources consumption. So I don't think you will get much efficency increase.

Also you can try to start thread manually with new Thread(), this should help you to start many threads immidately, but agian I don't think you will get much efficency increase.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top