Hi All,
I have copied from JLS below:
A single
thread may hold a lock more than once.
The example:
class
Test {
public static void main(
String[] args) {
Test t = new Test();
synchronized(t) {
synchronized(t) {
System.out.println("made it!");
}
}
}
}
prints:
made it!
This example would deadlock if a single thread were not permitted to lock a lock more than once.
1. I am unable to understand what is said above. How does a deadlock occur here?
2. Why does a thread need more than one lock on a single object/multiple objects?
3. In what situations a thread need more than 1 lock ?
please explain.
Thanks,
Priyha.