• Post Reply Bookmark Topic Watch Topic
  • New Topic

Lock on an object

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when a lock on an object is attained by one thread when it run a synchronized method, the other thread of the same object can run the other non synchronized methods of the class without any restriction. Is this true?Please tell me the reasoning coz i believe that the lock is attained on an object in this case and other thread cannot enter any of the methods then
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Synchronization is cooperative -- when you acquire a lock, other threads trying to acquire the same lock must wait for you to release it. In other words, the only time you need to wait for a lock is when you are trying to acquire it.

So, when you call a non-synchronized method, you are not trying to acquire a lock, and hence, should not block waiting for it.

Henry
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Sonia wrote:coz

http://faq.javaranch.com/java/UseRealWords

Non-synchronized methods don't need the lock, so why should you block access to them?
 
Campbell Ritchie
Marshal
Posts: 52608
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for us beginners. Moving.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!