1

As per my understanding async/await will use ThreadPool thread for performing asynchronous operation and we prefer Threadpool thread when operation will be done within shorter span of time, so threadpool threads will be free early.

So if we use async/await or Task for downloading huge amount of data, then whether it will impact on application performance since threadpool thread will not be free early and Threadpool will have to create new thread(which is expensive operation).

One more thing, if async/await is not preferable in above scenario, what should be alternative to download huge amount of data?? Should we create new thread explicitly.

Please share your thought and thanks in advance.....:):)

ranjit powar
  • 104
  • 2
  • 2
  • 10
  • 2
    > Threadpool will have to create new thread(which is expensive operation) - Wrong , threadpools use existing threads hence reducing thread creating time. And yes you need to use async.await for such a operation – Kavindu Dodanduwa Feb 05 '15 at 04:31
  • 1
    Note that *expensive* with todays computers may only be 1 or 2 ms; a veritable eternity for today's nano-second processors. – Pieter Geerkens Feb 05 '15 at 04:36
  • Threadpool will create new thread only if none of the thread in Threadpool is free. Lets say there are 5 threads in Threadpool and 2 threads are downloading files using async/await and remaining threads are performing some other operations. Now if new request come to threadpool and since none of the thread in Threadpool is free, it will create new thread. So we should not use Threadpool for operation which takes longer time to execute..... – ranjit powar Feb 05 '15 at 04:39
  • @user978493 I'm pretty sure that if you queue a work item on the pool and there are no threads free then the work is _queued_ until a thread is free. It is possible to oversubscribe the thread pool. This question illustrates how you can easily use `Task` to create new threads, specifically intended for long running work http://stackoverflow.com/questions/3105988/taskcreationoptions-longrunning-option-and-threadpool – Gusdor Feb 05 '15 at 08:26

3 Answers3

2

Async IO does not use threads while it runs. That's the point.

Async IO does not make an IO faster. It only changes the way it is started and completed. It will gain you zero performance for your big file download.

usr
  • 168,620
  • 35
  • 240
  • 369
0

Some correction, According to the document and as I have explained ThreadPools do not have the overhead of creating threads. Hence it provide the advantage of avoiding "thread creating overhead and thread disposing".

Quoted :

Thread pools typically have a maximum number of threads. If all the threads are busy, additional tasks are put in queue until they can be serviced as threads become available.

So yes, having MANY downlaods simultaniously COULD outnumber available number of threads in ThreadPool

Finally your main question : Yes async/await is a good solution for a file download. A good tutorial i used sometime back.

Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46
0

You should absolutely use async/await in this case. Using async/await does not block the calling thread so it does not cause creation of new threads.

The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread.

And you are asking about IO operation, using async/await is a perfect fit for this:

The async-based approach to asynchronous programming is preferable to existing approaches in almost every case. In particular, this approach is better than BackgroundWorker for IO-bound operations because the code is simpler and you don't have to guard against race conditions.

The MSDN article has more details.

Mert Akcakaya
  • 3,109
  • 2
  • 31
  • 42