I'm running following code in 4 threads:
public static void Run()
{
Random rnd = new Random();
lock (locker)
{
Thread.Sleep(1000);
Console.WriteLine(rnd.NextDouble());
}
}
This is how I run it:
static object locker = new object();
static List<Thread> threadList = new List<Thread>();
static void Main(string[] args)
{
for (int i = 0; i < 4; i++)
threadList.Add(new Thread(Run));
foreach (Thread thread in threadList)
thread.Start();
Console.ReadKey();
}
Every time at least two threads print same number in console. How may this happen?
Aucun commentaire:
Enregistrer un commentaire