I've just asked question about Task but realized that I actually want to ask more general question. Could someone summarize pros and cons of Tasks and Threads. How to understand should I use Task or Thread?
Asked
Active
Viewed 1.1k times
17
-
1I've answered a similar (but not quite the same) question [here](http://stackoverflow.com/a/9493446/960195). Hope it comes in handy. – Adam Mihalcin Mar 15 '12 at 20:28
-
@AdamMihalcin I'm looking for algorithm which can answer what should I use in each particular case - Task or Thread? – Oleg Vazhnev Mar 15 '12 at 20:33
-
1http://stackoverflow.com/questions/9493421/different-between-task-system-threading-task-and-thread#comment12019624_9493421 – dlev Mar 15 '12 at 20:33
-
14The first thing to understand is *what the difference is between a task and a thread*. "Make me a sandwich" is a task. Hiring a chef is creating a thread. If you confuse those two things then you are going to either hire a whole lot of unnecessary chefs that you then cannot eat, or you are going to be asking a pile of sandwiches to make you dinner. The way you decide whether to use a task or a thread is to decide whether you want to represent *the task* in code *irrespective of who performs the task*, or whether you want to represent *the worker* that performs the task. – Eric Lippert Mar 15 '12 at 20:33
-
[This](http://stackoverflow.com/q/4130194/884410) may help – Raj Ranjhan Mar 15 '12 at 20:37
-
@EricLippert it seems msdn suggest to use Task even as "workers": "in the .NET Framework 4, tasks are the preferred API for writing multi-threaded, asynchronous, and parallel code." – Oleg Vazhnev Mar 15 '12 at 20:38
-
4@javapowered: They are the preferred way to go, because they refocus your code to emphasize *the tasks being performed* and not *the workers that are performing them*. Most problems with threading code arise from the difficulties of coordinating the workers -- the threads. If you are in the business of eating sandwiches, it is better to be able to say "I need a hundred sandwiches" and let the task library work out how many cooks to hire. – Eric Lippert Mar 15 '12 at 20:41
-
@javapowered: The reason Tasks are recommended is because rarely are people concerned with the chef (to follow Eric's example), they just want a sandwich. So recommending Task as default gets the majority of people what they want. However if you need a chef, you need a chef, and will by no means be hurt by using Threads in that case. – Guvante Mar 15 '12 at 20:43
-
http://stackoverflow.com/questions/4130194/what-is-the-difference-between-task-and-thread – bytedev Mar 25 '14 at 15:43
1 Answers
5
Task
is an order to program to do something in asynchronous way. The Thread
is actually OS
kernel object which executes what was requested. Think about Task
like a clever thread aggregator/organizer that "knows" how much task is better to run contemporary on your CPU
. It's just cleverer then common implementations of multi-threading (that's why it's suggested choice from Microsoft). It's a feature that helps you managing Threads
in easier way.
Look also on this Should i use ThreadPools or Task Parallel Library for IO-bound operations that may give you some hints on performance issues you may be interested in.