What is the difference between task and thread




















Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. The Thread class is used for creating and manipulating a thread in Windows. A Task represents some asynchronous operation and is part of the Task Parallel Library, a set of APIs for running tasks asynchronously and in parallel.

The task can return a result. There is no direct mechanism to return the result from a thread. Task supports cancellation through the use of cancellation tokens. You can see a Task as a higher level abstraction of threads, which could be a reason why they are under the System.

Threading namespace. You can accomplish the same with a thread as you can with a task. Take a look at the following examples, the first example is using a thread:. In the above the use of Task is a lot simpler and has no thread jargon.

You create a new task and wait synchronously for the result using await. Normally a Task is all you need, I cannot remember the last time I had to use a thread for something other than experimentation. You can't even tell when the pool will start running the work you submit to it. Using ThreadPool avoids the overhead of creating too many threads. However, if you submit too many long-running tasks to the threadpool, it can get full, and later work that you submit can end up waiting for the earlier long-running items to finish.

In addition, the ThreadPool offers no way to find out when a work item has been completed unlike Thread. Join , nor a way to get the result. Therefore, ThreadPool is best used for short operations where the caller does not need the result. Finally, the Task class from the Task Parallel Library offers the best of both worlds. Like the ThreadPool, a task does not create its own OS thread. Instead, tasks are executed by a TaskScheduler; the default scheduler simply runs on the ThreadPool.

Unlike the ThreadPool, Task also allows you to find out when it finishes, and via the generic Task to return a result. You can call ContinueWith on an existing Task to make it run more code once the task finishes if it's already finished, it will run the callback immediately. If the task is generic, ContinueWith will pass you the task's result, allowing you to run more code that uses it.

You can also synchronously wait for a task to finish by calling Wait or, for a generic task, by getting the Result property. Like Thread. Join , this will block the calling thread until the task finishes.

Synchronously waiting for a task is usually bad idea; it prevents the calling thread from doing any other work, and can also lead to deadlocks if the task ends up waiting even asynchronously for the current thread. Since tasks still run on the ThreadPool, they should not be used for long-running operations, since they can still fill up the thread pool and block new work.

What is Thread? Why do we need Task? Why do we need Thread? NET framework provides Threading. Tasks class to let you create tasks and run them asynchronously. A task is an object that represents some work that should be done. The task can tell you if the work is completed and if the operation returns a result, the task gives you the result. NET Framework has thread-associated classes in System. Threading namespace. A Thread is a small set of executable instructions.



0コメント

  • 1000 / 1000