Difference between Task and Thread

Asked

Viewed 5,828 times

31

I need to create an executable c# and the question was: Do with thread or do with task?

  • In practice there is some difference between using Task and the "traditional" multi-thread??
  • It is true that a task may have several threads?

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

As they deal with individual calls, I want to prevent one from running only when the other ends and simply run them all at the same time.

1 answer

25


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

  • Task is something more abstract. It’s something that was created on . NET so the programmer doesn’t have to deal with the details of parallelism or asynchronicity. When using a task you are saying that you need something ready at some future time. How this will be accomplished is something that the framework can decide how to do better. In general he is able to do this. We can consider it as business rule (perform asynchronously).

Have a reasonable chance of task use one or more threads internal to achieve the goal, be creating threads or using existing. Either way, even when talking about creation, most likely will be done through a pool managed by framework.

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

Task is more powerful

There are a number of tools in the API of Task to use resources more easily, correctly and effectively. Control and communication between tasks is much better. Everything that would need to be done with threads for good use is already ready and has been carried out by a team that understands the subject and has been able to test properly.

It is common to use tasks associated with async.

How it will be used in any specific application or which one is better is always difficult to say for sure, especially without too many details about the requirements. But what is recommended today on . NET is to use task by default and only if it does not provide all that is expected to pass to thread or another more concrete way of achieving a future result that does not block the application.

Specific example

It’s a little strange this definition of 5 layers that can be performed at the same time. I don’t know if this is true, or even if it needs to be said, but considering that the description seems to require only asynchronicity, so just ask for tasks to be performed, if you need to create threads, to TPL create. TPL has everything you need to run in parallel.

I strongly recommend reading this another question. Can also read something about the difference in terms. That one article is also good.

In general that is, when you are applying may arise specific questions that we can answer.

  • Excellent, bigown ! Thank you very much!! If you’re not going to abuse your good will, is there any way to explain how the asynchrony works in the Task? And the idea is execute 5 methods different... In thread I would put the threads in one Collection, start all together and then go through one by one to know which one is still active...

  • I understand, so they’re not layers. I’ll go if it’s complementary in the answer or if you need a new question for complexity. To tell you the truth I don’t know much about internal functioning, I know it’s complex. You read the question await/async?

  • I wrote "call" when I meant execute rs... I read yes... await already avoids that I need to go through a list and do the individual execution... I can do 5 executions each with your await?

  • Yes, you can, as many as you want, and if you need thread to ensure this, is a problem of Task internally solve this, neither the code called, nor the caller needs to know this.

  • Perfect! Thanks again :)

  • 1

    if you want/can take a look at this question: [Multiple connections] (http://answall.com/questions/125172/m%C3%Baltiplas-conex%C3%B5es-com-banco-execut%C3%A1vel-c-com-multi-task) - is directly related to this question :)

  • @Maniero Exists in [tag:operating systems] (in the open book cited in the tag has explanation) the concept of "task" or "task", which is more abstract (a sequence of instructions to be executed one after the other), and is not specific to . NET, although possibly Task whether it is an abstraction on top of this concept, direct or with some extra intention, perhaps to isolate the form of programming from the OS execution model, which today is already an attempt to promote abstraction for the execution of tasks (with core threads, user threads and processes). Perhaps the choice of thread model breaks this abstraction.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.