Difference between Task and new Thread

Task: is way of doing async which is same like promose in javascript . internally it’s using ThreadPool to schedule the task .and manage some states to make async program easier (like consolidate exceptions ) . more important ,Task will try utilize the best of CPU to get the ‘task’ done .

Below are some code take from .net core .let’s take a close looking at what is a ‘Task’ in .net

public partial class Task : IThreadPoolWorkItem, IAsyncResult
{
internal static int s_taskIdCounter;
private static TaskFactory s_factory = new TaskFactory();

private int m_taskId;

internal object m_action;

internal object m_stateObject;
internal TaskScheduler m_taskScheduler;

internal readonly Task m_parent;
internal ExecutionContextLightup m_capturedContext;

internal volatile int m_stateFlags;

private volatile ManualResetEventSlim m_completionEvent;

internal class ContingentProperties
{
public volatile int m_internalCancellationRequested;
internal volatile int m_completionCountdown = 1;
public volatile TaskExceptionHolder m_exceptionsHolder;
public volatile List m_exceptionalChildren;
public volatile List m_continuations;
public CancellationToken m_cancellationToken;
public Shared m_cancellationRegistration;
}

internal class Shared
{
internal T Value;
internal Shared(T value) { Value = value; }
}

internal volatile ContingentProperties m_contingentProperties;

Below code calling Task Scheduler

public void Start(TaskScheduler scheduler)
{
ScheduleAndStart(true);
}
internal void ScheduleAndStart(bool needsProtection)
{
...
m_taskScheduler.QueueTask(this);
...
}
}

TaskScheduler.cs

internal sealed class ThreadPoolTaskScheduler : TaskScheduler
{
private WaitCallback m_wcCallback;
private Action< object > m_ptsCallback;

internal ThreadPoolTaskScheduler()
{
m_wcCallback = state => TryExecuteTask((Task)state);
m_ptsCallback = state => TryExecuteTask((Task)state);
}

protected internal override void QueueTask(Task task)
{
if ((task.CreationOptions & TaskCreationOptions.LongRunning) != 0)
{
ThreadLightup thread = ThreadLightup.Create(m_ptsCallback);
thread.IsBackground = true;
thread.Start(task);
}
else
{
ThreadPool.QueueUserWorkItem(m_wcCallback, task);
}
}
...
}

Start New Thread : it does nothing more than allocate a thread in .net application process (but may in different AppDomain). yes, it will allocate resources in thread and there will be a context switch happen .

Conclusion :
Thhread and Task is for different abstraction purpose .Start new thread is to do something in another stand-alone env (system resource are still sharing), the focus is isolate the problem we are solving;whereas the focus of Task to avoid blocking I/O , work with a callback function . Task is a combination of thread, few locks ,callbacks, etc. it’s to provide api so that developer could focus on the ‘task’ he is working on ;Thread need to be managed(synchronise, lock) manully .but you have options to control the thread priority and so on .

Author: lanliang

Programmer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s