If I use synchronize (this) in two methods and one calls the other, would I be stuck in a deadlock situation or would it work because the thread already owns the lock?
Figure class below:
public class Test { public void foo() { synchronize(this) { bar(); } } public void bar() { synchronize(this) {
As you can see, there are two methods foo and bar, which both rely on synchronization.
Calling foo () on (this) will get a lock; will prevent you from trying to do the same when calling foo (and thereby cause a deadlock), or will he realize that the lock is already received by the same thread?
Hope my explanation is more or less clear :-)
source share