lock 是什么?
public void UseMultiThread2()
{
Console.WriteLine("Child Thread 2 Started");
Console.WriteLine("Child Thread 2 - Counting from 11 to 20");
for (int i = 11; i <= 20; i++)
{
for (int a = 0; a < 20; a++)
{
Console.Write(".");
}
Console.WriteLine(i);
}
Console.WriteLine("Child Thread 2 finished");
Monitor.Enter(this);
Console.WriteLine("Child Thread 2 Started");
Console.WriteLine("Child Thread 2 - Counting from 11 to 20");
for (int i = 11; i <= 20; i++)
{
for (int a = 0; a < 20; a++)
{
Console.Write(".");
}
Console.WriteLine(i);
}
Console.WriteLine("Child Thread 2 finished");
Monitor.Exit(this);
lock (this)
{
Console.WriteLine("Child Thread 2 Started");
Console.WriteLine("Child Thread 2 - Counting from 11 to 20");
for (int i = 11; i <= 20; i++)
{
for (int a = 0; a < 20; a++)
{
Console.Write(".");
}
Console.WriteLine(i);
}
Console.WriteLine("Child Thread 2 finished");
}
}