• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread's Locks

 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would this be intersting ?!!
Any ideas ?

14.18 The synchronized Statement
......
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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a single thread was not be able to acquire the same lock multiple times, it would not be permitted to invoke a synchronized method from the body of another synchronized method of the same class. In clear, one thread is allowed to lock the same lock multiple times. The lock is released when a equal amount of unlock actions have been performed.
Please refer to JLS 17.5 Rules about Locks for detailed information about how un/locking works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic