C# program that uses lock statement
static readonly object _object = new object();
static void A()
// Method A: lock on the readonly object.
// ... Inside the lock, sleep for 100 milliseconds.
// ... This is thread serialization.
static void Main()
// Create 10 new threads.
for (int i = 0; i < 10; i++)
ThreadStart start = new ThreadStart(A);
IL. Let's examine the intermediate representation for the lock statement. In compiler theory, high-level source texts are translated to lower-level streams of instructions.IL
Tip: The lock statement here is transformed into calls to the static methods Monitor.Enter and Monitor.Exit.
Also: The lock is actually implemented with a try-finally construct. This uses the exception handling control flow.