No, the lock is only about
x
. The wait will occure if a thread is inside the
lock
block, and another thread is trying to enter the lock at the same time.
To make method2 block, you should write something like this:
public void method1()
{
lock(x)
{
}
}
public void method2()
{
lock(x)
{
...
}
}
-------------------------------
But, my question is that if method2() hasn't got a lock statement, will it have to wait?
If there is no lock in
method2
, it will not wait.
Now about puting a lock or not, it depends what you do in
method1
and
method2
.
If you put the lock in
method1
to protect a few members, and you access the same members in
method2
, then you will probably have to use a lock in
method2
too.
If you do something very different, then you don't need to lock in
method2
.