• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confusion about object lock.

 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a class as shown below:



this class has two methods they are synchronized. Now let us say thread 1 is executing firstMethod while its executing that thread 2 tries to execute the second method on same object. Will it be able to execute it or not? If yes then how come this is lock on a object? I hopw this is not a silly question. Please help.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a synchronized method of an object is called, all other synchronized of the same object will block until the executed method ends.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example where the first method sleeps 5 seconds. Check what happens with and without the synchronized keyword on the second method.
 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Christophe!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic