• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on synchronized methods  RSS feed

 
Steven Squeers
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could somebody confirm my understanding of synchronized methods please?
I was wondering, if I declare an instance method as synchronized, if that locks the instance against concurrent modifications, or just prevents concurrent access to that method on the instance on which the method is called.
I have put together a test that (if correct) says the instance itself is not protected against concurrent modifications; only entry into the method is protected against concurrent execution.
Is that correct?

Thanks.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronizing a method means that no other synchronized method for the object can begin until that method has completed. That's also true of any block of code in any class that is synchronized on the same object. Synchronizing doesn't magically prevent concurrent modifications though. Non-synchronized methods or blocks can run while the synchronized one is still in process, so it's up to the developer to make sure all access to shared resources is done through synchronized blocks and methods.
 
Steven Squeers
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote: That's also true of any block of code in any class that is synchronized on the same object.


Are you saying that, if there is a synchronized method foo in class X, and a synchronized block of code in an entirely separate class - Y - that is synchronized on an X object, that if two threads share the same reference to an X - say x - then if one of them is executing foo() in X using instance x, then the other will have to wait for foo() to return before it can execute the synchronized code block in class Y?




 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a synchronized non-static method behaves exactly like a block synchronized on the "this" object. You can synchronize on an object anywhere you want, not just within instance methods of the object's class. Each object has something called a monitor, which you can think of like a baton to give it some real representation. Whenever a thread wants to enter a synchronized block of code, it needs to determine what object the block is synchronized on, and then get that object's baton (i.e., monitor). When it exits the block, it gives back the monitor. So, if one thread is in the middle of a synchronized block and another thread wants to enter the same block or any other block synchronized on the same object, it goes to the object to get the monitor, but the monitor isn't there! No monitor, no honey! The second thread has to wait until the first thread finishes the block it was in and releases the object monitor.
 
Steven Squeers
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great Greg, many thanks
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!