Answer the question
In order to leave comments, you need to log in
What are the pros and cons of Task and Thread?
Google tells me that the Task class is a higher-level, modern and less resource-intensive way to write programs using multithreading and is better to use only, but Thread should be forgotten altogether.
Today I was at an interview where people have been writing highly efficient, high-load solutions in C # for many years. And when I said "Task is better than Thread" they looked at me like I was an idiot.
Apparently I don't understand something. Tell me what and for what purposes is better?
Answer the question
In order to leave comments, you need to log in
In essence, a task is a pool of threads that do not need much monitoring and are easier to use, and they can also return values. They can be used, for example, for requests to the server, for unloading the main thread - for example, something difficult to calculate. Thread is faster, but task gives more options and less attention to itself
Jon Skeet himself answers:
https://stackoverflow.com/a/13429164/3143750
Well, here's another
https://www.c-sharpcorner.com/article/task-and-thr...
In simple words:
Thread is a separate thread in which you want to execute something in parallel from other threads.
(for example, so that the interface does not freeze, you run your task in a separate thread)
(for example, you want to use all the processor cores and run several tasks in several parallel threads)
Task is a wrapper over Thread that allows you to reuse the created threads (Thread Pool).
Reuse because creating a Thread each time is very time consuming and expensive.
Tasks also provide additional features such as:
- asynchronous model (async / await),
- accounting for the number of processor cores,
- accounting for downtime during system calls (network, file system, etc.)
In any case, Task is the best solution.
Thread is an abstraction of a thread. Task is a higher level abstraction; it is a Promise, in other words, some operation that will be completed in the future.
And Google is right, Thread is not used directly in the code now.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question