I\'m trying to understand why this code is not working.
private static object _lock;
public static void Main (string[] args)
{
Thread th
Your lock does not do anything at all; only one thread is locking on that object - the one that is starting the others. Those other threads never contest for that lock at all.
Because each thread is refering to the loop variable, and does not get its own copy at the time you create the thread.
Notice that the compiler is warning you: "Access to a modified closure".
foreach (int num in Enumerable.Range(0,5))
{
int loopnum = num;
thread = new Thread(() => print(loopnum.ToString()));
thread.Start();
}