Task VS Thread Differences

What is the difference between task and thread?

A task is something you want done.

A thread is one of the many possible workers which performs that task.

In .NET 4.0 terms, a Task represents an asynchronous operation. Thread(s) are used to complete that operation by breaking the work up into chunks and assigning to separate threads.

Task vs Thread differences

Thread is a lower-level concept: if you're directly starting a thread, you know it will be a separate thread, rather than executing on the thread pool etc.

Task is more than just an abstraction of "where to run some code" though - it's really just "the promise of a result in the future". So as some different examples:

  • Task.Delay doesn't need any actual CPU time; it's just like setting a timer to go off in the future
  • A task returned by WebClient.DownloadStringTaskAsync won't take much CPU time locally; it's representing a result which is likely to spend most of its time in network latency or remote work (at the web server)
  • A task returned by Task.Run() really is saying "I want you to execute this code separately"; the exact thread on which that code executes depends on a number of factors.

Note that the Task<T> abstraction is pivotal to the async support in C# 5.

In general, I'd recommend that you use the higher level abstraction wherever you can: in modern C# code you should rarely need to explicitly start your own thread.

What is the difference between a thread/process/task?


Process:

A process is an instance of a computer program that is being executed.
It contains the program code and its current activity.
Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.
Process-based multitasking enables you to run the Java compiler at the same time that you are using a text editor.
In employing multiple processes with a single CPU,context switching between various memory context is used.
Each process has a complete set of its own variables.

Thread:

A thread is a basic unit of CPU utilization, consisting of a program counter, a stack, and a set of registers.
A thread of execution results from a fork of a computer program into two or more concurrently running tasks.
The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process. Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources.
Example of threads in same process is automatic spell check and automatic saving of a file while writing.
Threads are basically processes that run in the same memory context.
Threads may share the same data while execution.
Thread Diagram i.e. single thread vs multiple threads

Task:

A task is a set of program instructions that are loaded in memory.

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?"

Difference between Task (System.Threading.Task) and Thread

I think that what you are talking about when you say Task is a System.Threading.Task. If that's the case then you can think about it this way:

  • A program can have many threads, but a processor core can only run one Thread at a time.

    • Threads are very expensive, and switching between the threads that are running is also very expensive.
    • So... Having thousands of threads doing stuff is inefficient. Imagine if your teacher gave you 10,000 tasks to do. You'd spend so much time cycling between them that you'd never get anything done. The same thing can happen to the CPU if you start too many threads.

To get around this, the .NET framework allows you to create Tasks. Tasks are a bit of work bundled up into an object, and they allow you to do interesting things like capture the output of that work and chain pieces of work together (first go to the store, then buy a magazine).

Tasks are scheduled on a pool of threads. The specific number of threads depends on the scheduler used, but the default scheduler tries to pick a number of threads that is optimal for the number of CPU cores that you have and how much time your tasks are spending actually using CPU time. If you want to, you can even write your own scheduler that does something specific like making sure that all Tasks for that scheduler always operate on a single thread.

So think of Tasks as items in your to-do list. You might be able to do 5 things at once, but if your boss gives you 10000, they will pile up in your inbox until the first 5 that you are doing get done. The difference between Tasks and the ThreadPool is that Tasks (as I mentioned earlier) give you better control over the relationship between different items of work (imagine to-do items with multiple instructions stapled together), whereas the ThreadPool just allows you to queue up a bunch of individual, single-stage items (Functions).

Performance comparison between Thread and Task using c#

Executing a method the first time is always more costly: assemblies are lazily loaded, and the method may not be JITted yet.

For instance, if we take your benchmark (replacing DateTime by Stopwatch for precision) and call Task.Factory.StartNew once more:

static void Main(string[] args)
{
var sw = Stopwatch.StartNew();

new Thread(() => { ExecuteAsyn("Thread", sw); }).Start();
Console.WriteLine("Using Thread: " + sw.Elapsed);

sw = Stopwatch.StartNew();

Task g = Task.Factory.StartNew(() => ExecuteAsyn("TPL", sw));
Console.WriteLine("Using TPL: " + sw.Elapsed);

g.Wait();

sw = Stopwatch.StartNew();

g = Task.Factory.StartNew(() => ExecuteAsyn("TPL", sw));
Console.WriteLine("Using TPL: " + sw.Elapsed);

g.Wait();

Console.ReadKey();
}

private static void ExecuteAsyn(string source, Stopwatch sw)
{
Console.WriteLine("Hello World! Using " + source + " the difference between initialization and execution is " + (sw.Elapsed));
}

The result on my computer is:

Using Thread: 00:00:00.0002071

Hello World! Using Thread the difference between initialization and execution is 00:00:00.0004732

Using TPL: 00:00:00.0046301

Hello World! Using TPL the difference between initialization and execution is 00:00:00.0048927

Using TPL: 00:00:00.0000027

Hello World! Using TPL the difference between initialization and execution is 00:00:00.0001215

We can see that the second call is faster than the first one by three orders of magnitude.

Using a real benchmark framework (for instance, BenchmarkDotNet), we can get much more reliable results:

  Method |       Mean |     Error |    StdDev |
-------- |-----------:|----------:|----------:|
Threads | 137.426 us | 1.9170 us | 1.7932 us |
Tasks | 2.384 us | 0.0322 us | 0.0301 us |

That said, a few additional remarks:

  1. Your comparison is not fair. You're comparing the creation of a thread versus enqueuing a task on the threadpool via the tasks APIs. For fairness, you should use ThreadPool.UnsafeQueueWorkItem instead (which allows you to use the threadpool without the task API)

  2. Whether to use Thread or Task shouldn't be a matter of performance. It's really more a matter of convenience. It's highly unlikely that the performance gap would make any difference in your application, unless you're dealing with low-latency or very high throughput.

Should I notice a difference in using Task vs Threads in .Net 4.0?

There are various implications to using Tasks instead of Threads, but performance isn't a major one (assuming you weren't creating huge numbers of threads.) A few key differences:

  1. The default TaskScheduler will use thread pooling, so some Tasks may not start until other pending Tasks have completed. If you use Thread directly, every use will start a new Thread.
  2. When an exception occurs in a Task, it gets wrapped into an AggregateException that calling code can receive when it waits for the Task to complete or if you register a continuation on the Task. This is because you can also do things like wait on multiple Tasks to complete, in which case multiple exceptions can be thrown and aggregated.
  3. If you don't observe an unhandled exception thrown by a Task, it will (well, may) eventually be thrown by the finalizer of the Task, which is particularly nasty. I always recommend hooking the TaskScheduler.UnobservedTaskException event so that you can at least log these failures before the application blows up. This is different from Thread exceptions, which show up in the AppDomain.UnhandledException event.


Related Topics



Leave a reply



Submit