Thread Vs Async | Time Comparision
Hi Thread takes less time. That's why one should choose thread over Async. Is this correct? I am getting more time for execution for Async compared to thread for below code: https://code.sololearn.com/cA139a72a19A Why should I opt for Async?
2/27/2021 2:52:43 PMKetan Lalcheta
7 AnswersNew Answer
Say for example, you are making a request to a server. Now there will certainly be a delay in recieving the response because the server will first process the request, and migt perform actions that can take upto several seconds before giving back the response. This is the important thing here - no matter how much processing power your system has or how many threads you use, you cannot decrease the delay between sending the request and recieving the response (because it's in the hand of the server). So here threads are not of much use. Instead, if you use async, you can actually do other work while waiting for the response from the sever. So now, even if the server takes a lot of time (upto even 5 seconds or so), you don't need to wait in that time. Now, you could say that you can make 2 threads one of which performs the other tasks and one that keeps waiting for the response. But it is a real waste of resources to have a thread that is just waiting and doing nothing else.
[Part 1 of answer] Thread does not always take less time. It depends on what you're doing. Let's take an example. Imagine you are chopping vegetables. Surely, it will take less time if you have another person or maybe 3 more people chopping vegetables with you. That is, this task can be done in less time if you have 2 (or more) threads doing it. Hence, multithreading will be preferred in this task. Now imagine that you need to prepare toast and do some other work. Now after you put the bread in the toaster, you will need to wait for some time. Even if you bring 5 more people to help you, you have no choice but to wait till the toast is ready. Instead, if you put the bread in the toaster and till it is finshed, do the other work, you will be saving time as well as the resources and management needed for more than 1 people. (to be continued)
[Part 2 of answer] So that is why, in a case like this, where you have jobs that require waiting and they can be completed *asynchronously* (like putting bread into the toaster and while that task is being completed, doing some other tasks) , then there is no point in using threads as the time for creating them, the resources and management they will need is just a waste. Here, async is the preferred way. I had seen an example similar to this in some article but I can't find it now and so I can't source it. Reasons for choosing async over multi-threading: https://stackoverflow.com/questions/4024056/threads-vs-async/4024385 https://youtu.be/kdzL3r-yJZY
Ketan Lalcheta yes, I think five threads handling each task is a good choice as long as those tasks are independant of each other's completion. But as for the subtasks, I think there is no need of any sort of concurrency. As each sub task can be completed only when the previous one is over, concurrency had no use here. You can simply do all the subtasks synchoronously. Making threads for the subtasks will only waste resources because most of the time the threads will be sitting idle and also be constantly checking if the previous task is over. So I think having no threads just eliminates the need to worry about races. Did I understand your question correctly? (Also, I want to make it clear that I have don't have mch experience with concurrency. I have read a number of articles and that is why I have the basic knowledge but I am not really confident in it. So if I said something incorrect here, anyone can feel free to correct me)
Hi XXX , best ever example I came across till date for async vs thread... Thank you so much.... I had miscocept that thread can be increased to break the task but it seems wrong ... Let me clear my one more assumption.... I have a task which need three sub tasks.... Each sub task can be completed once previous is over.... Also there are such 5 tasks.... In this case , is it always best to go for five thread each handling one task and wait till individual corresponding sub task is not finished ? Another query is each sub task of task can be shared by each thread with racing condition but it is not good as second sub task thread always wait till first sub task is not over... In this case , racing and hence condition variable is of not that much importance... Once again thanks for your time and help and wonderful example
Yes, you got question right...! And many thanks for sharing your knowledge. It helps. I just now updated my existing code with third scenario that is no thread no async. This is shortest time in current example as expected. Hence, I am clear that no dependency , use thread to achieve faster ...but again confused on when async can be faster than normal scenario? I want to know one scenario when async takes less time than normal scenario and threading as well. Again thanks a lot to all of you...!
threads could be faster than async because they provide more real multi-task handling... but coding for thread is hardest than coding async... so it mostly depends on how and what you're coding ^^ (roughly: async share one cpu core for multi-task in parrallel, while threads share one cpu by thread...)