I read, in many many articles, that multiple threads will "compete" for the same resource and try to recreate it with the following code:
static void Main(string[] args)
{
List<int> list = new List<int>();
Thread t1 = new Thread(thread1);
t1.IsBackground = true;
t1.Start();
Thread t2 = new Thread(thread2);
t2.IsBackground = true;
t2.Start();
Thread.Sleep(3000);
foreach(int i in list)
{
Console.WriteLine(i);
}
Console.ReadLine();
void thread1()
{
for (int i = 0; i < 10000; i++)
{
list.Add(i);
}
}
void thread2()
{
for (int i = 10000; i < 20000; i++)
{
list.Add(i);
}
}
}
But the result is still very "nice and neat" in order (from 0 to 19999), what did I do wrong?