What is the difference between task and thread?

后端 未结 8 1602
庸人自扰
庸人自扰 2020-11-22 02:45

In C# 4.0, we have Task in the System.Threading.Tasks namespace. What is the true difference between Thread and Task. I did s

8条回答
  •  遇见更好的自我
    2020-11-22 03:32

    In addition to above points, it would be good to know that:

    1. A task is by default a background task. You cannot have a foreground task. On the other hand a thread can be background or foreground (Use IsBackground property to change the behavior).
    2. Tasks created in thread pool recycle the threads which helps save resources. So in most cases tasks should be your default choice.
    3. If the operations are quick, it is much better to use a task instead of thread. For long running operations, tasks do not provide much advantages over threads.

提交回复
热议问题