I have a simple code which has a lock and creates 5 threads.
static readonly object _object = new object();
static void A(int currentValue)
{
lock (_object)
{
Console.WriteLine(currentValue + " Start");
Console.WriteLine(currentValue + " End");
}
}
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
Thread t = new Thread(() => A(i));
t.Name = "Thread " + i;
Console.WriteLine(t.Name + " Created");
t.Start();
}
Console.ReadKey();
}
When I run this code, the following is displayed in the output.
Thread 0 Created
Thread 1 Created
1 Start
1 End
Thread 2 Created
2 Start
2 End
Thread 3 Created
3 Start
3 End
Thread 4 Created
4 Start
4 End
5 Start
5 End
When I am only creating 5 threads (0 - 4). Why do I see an entry for 5 Start and 5 End?