When we execute things on multiple threads, it’s not guaranteed that the threads are separated across multiple processors.
Task is a lightweight object for managing a parallelizable unit of work. It can be used whenever you want to execute something in parallel. Parallel means the work is spread across multiple processors to maximize computational speed. Tasks are tuned for leveraging multicores processors.
Task provides following powerful features over thread.
- If system has multiple tasks then it make use of the CLR thread pool internally, and so do not have the overhead associated with creating a dedicated thread using the Thread. Also reduce the context switching time among multiple threads.
- Task can return a result. There is no direct mechanism to return the result from thread.
- Wait on a set of tasks, without a signaling construct.
- We can chain tasks together to execute one after the other.
- Establish a parent/child relationship when one task is started from another task.
- Child task exception can propagate to parent task.
- Task support cancellation through the use of cancellation tokens.
- Asynchronous implementation is easy in task, using’ async’ and ‘await’ keywords.
No comments:
Post a Comment