Saturday, 14 September 2013

thread synchronised confused by lock(this)

thread synchronised confused by lock(this)

I hava a class:
public class LockTest
{
public void LockThis()
{
lock (this)
{
Console.WriteLine("lock this test");
System.Threading.Thread.Sleep(5000);
}
}
}
in Main:
static void Main(string[] args)
{
LockTest lockTest = new LockTest();
lock (lockTest)
{
//lockTest.LockThis();
System.Threading.Thread thread = new Thread(lockTest.LockThis);
thread.Start();
}
Console.Read();
}
I thought the invoking lockTest.LockThis() will cause a dead lock but it
didn't. I don't konw why.

No comments:

Post a Comment