Even if I am using TPL from long time but since it sounds new to me. I want to understand the TPL with thread pool and I created a POC in .NET framework 4.0 for that which is as below.
public class CustomData
{
public long CreationTime;
public int Name;
public int ThreadNum;
}
public class TPLSample
{
public int MaxThread = 0;
public void Start()
{
Task[] taskArray = new Task[10000];
for (int i = 0; i < taskArray.Length; i++)
{
taskArray[i] = Task.Factory.StartNew((Object obj) =>
{
var data = new CustomData() { Name = i, CreationTime = DateTime.Now.Ticks };
Thread.SpinWait(10000);
data.ThreadNum = Thread.CurrentThread.ManagedThreadId;
if (Thread.CurrentThread.ManagedThreadId > MaxThread)
{
MaxThread = Thread.CurrentThread.ManagedThreadId;
}
Console.WriteLine("Task #{0} created at {1} on thread #{2}.",
data.Name, data.CreationTime, data.ThreadNum);
},
i);
}
Task.WaitAll(taskArray);
Console.WriteLine("Max no of threads {0}", MaxThread);
}
}
I found that only 14 threads are created to do this task!!
But why the 14? what is the criteria ? can I increase or decrease this number? How can I change this number. Is it really possible or totally abstracted from a developer.