Difference between Task and Thread

25

I need to create a C # executable and I have the doubts: Do with thread or do with task ?

  • In practice is there any difference between using Task and the "traditional" multi-thread ??
  • Is it true that a task can have multiple threads ?

Basically my executable will have 5 different calls, which can be executed at the same time.

As these are individual calls, I want to prevent one from executing only when the other completes and simply executing all at the same time.

    
asked by anonymous 13.04.2016 / 18:57

1 answer

20
  • Thread is something closer to concrete. Every .Net implementation basically follows what the operating system provides. You use when you need to deal specifically with thread . Note that it is common for some people to think of thread when it only wants parallelism or even asynchronicity. Thread is a way to achieve this, but not the only one. We can call it mechanism.

  • Task is something more abstract. It is something that was created in .Net for the programmer not having to deal with the details of parallelism or asynchronicity. When you use a task you are saying that you need something ready at some future time. How this will be accomplished is something the framework can decide how to do better. In general he is able to do this. We can consider it as a business rule (run asynchronously).

You have a reasonable chance of the task using one or more internal threads to achieve the goal, either by creating threads or using existing ones. Anyway, even when it comes to authoring, it will most likely be done through a pool managed by the framework .

An example of a difference in how the task chooses the best path: Thread.Sleep() consumes processing to wait a while, the Task.Delay() creates an interrupt on the processor (via the OS) for the code to be invoked.

Task is more powerful

There are a number of tools in the Task API to use resources more easily, accurately, and effectively. Control and communication between tasks is much better. Everything that needs to be done with threads for good use is already done and was done by a team that understands the subject and was able to test properly.

It's common to use tasks associated with async .

How it will be used in some specific application or which is better is always difficult to say with certainty, mainly without many details about the requirements. But what is recommended today in .NET is to use task by default and only if it does not provide everything that is expected to pass to thread or another more concrete way to get one future result that does not block the application.

Specific example

It's a bit strange this definition of 5 layers that can be executed at the same time. I do not know if this is true, or even if it needs to be said, but assuming it is, the description seems to require only asynchronicity, so just ask for tasks to be executed, if you need to create threads , the TPL will create. TPL has everything you need to run in parallel.

I strongly recommend reading this another question . You can also read something about the difference in terms . This article is also good.

That's the general rule, so when you're applying you may have specific questions that we can answer.

    
13.04.2016 / 19:28