Can we define synchronized block inside a synchronized method?

130 views Asked by At

In java can we define synchronized block inside a synchronized method? If so please explain me with example

1

There are 1 answers

1
luk2302 On

That is very much possible:

public class Locker {
    private final Object lock = new Object();
    synchronized void something() {
        synchronized (lock) {
            // tada
        }
    }
}

The usefulness of this is questionable though. This feels like it increases the risk of deadlocks by a huge margin.