Im have this simple task
void DoWork(string job)
That shoud bu performed many times in parrallel on Azure Worker Role . I have these options:
multi Asynchronous calls to DoWork
while(true) {DoWorkDelegate.BeginInvoke(...);}
Allocate by myself many threds
while(true) {Thread t = new Thread( DoWork); t.Start();}
Thread Pool
In terms of utilizations what is the recommanded method?
It is possible that this example is over simplified. In the examples shows a new thread would be spun up for each iteration in the loop. While I completely agree with @DanielMann to use TPL instead of threads, adding
while (true) {
Task.Factory.StartNew(() => DoWork());
}
would still spin up a lot of threads. Make sure there is a test for work to do outside of spinning up the threads like
while (true) {
job = FetchJobFromQueue();
if( job != null ){
Task.Factory.StartNew(() => DoWork(job));
}
else {
Thread.Sleep(1000);
}
}
Note the sleep time to keep from pegging the processor on an empty loop.
On a somewhat related note, take a look at this MSDN article for proper handling of that while(true) loop for exit conditions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.