What is difference between Task and Thread? What is difference between Task and Thread? multithreading multithreading

What is difference between Task and Thread?


what is diffrence between a task and a thread?

Suppose you are running a book delivery company. You have four cars and four drivers. A car is a thread, a driver is a processor, and a book delivery is a task. The problem you face is how to efficiently schedule the drivers and cars so that the tasks get done as quickly as possible.

Where things get weird is when there are more cars (threads) than drivers (processors). What happens then is halfway through a trip the driver parks one car (suspends the thread) and gets into a different car (switches context), drives that one around for a while performing tasks, and then eventually comes back to the first car. Obviously that is not as efficient as one driver staying in one car.

The idea of task-based parallism is to break up the work into small tasks that can produce results in the future, and then efficiently allocate exactly as many threads as there are processors so that you don't waste time context switching. In practice, it usually does not work out that nicely, but that's the idea.

which one is better, task or thread?

The question cannot be answered because it doesn't make any sense. Which is better, a book to deliver to a customer, or a car to deliver it in? A car is a device that can be used to deliver a book; those two things are not things you can sensibly describe as "better" or "worse" than the other. It's like asking "which is better, a hole or a drill?"


A "Task" is a piece of work that will execute, and complete at some point in the future.

A "Thread" is how something gets executed.

Typically, when you create a Task, by default (ie: using Task.Factory.StartNew), the Task will get Scheduled to run upon a ThreadPool thread at some point. However, this is not always true.

The advantage of making this separation is that you are allowing the framework (or yourself, if you use a custom TaskScheduler) to control how your work gets mapped onto available threads. Typically, you'll have many more work items than threads - you may have one million items to process, but only 8 cores in your system. In a situation like this, it's much more efficient to use a fixed number of threads, and have each thread process multiple items of work. By separating "Task" from "Thread", you're breaking this coupling of work==thread.

In general, I would recommend using Task instead of creating your own threads. This is a much nicer, more powerful, and flexible model to use for development, especially as it allows you to handle exceptions in a very clean manner, allows nice things like continuations to be generated, etc.


Here are some differences:

  • Task by default uses thread pool while directly using Thread will require creating new thread.
  • Task will handle the exceptions and results so using it easier.
  • Task can support cancellation while using threads, you have to implement it yourself.