• Post Reply Bookmark Topic Watch Topic
  • New Topic

When would you use synchronized methods as opposed to synchronized blocks?  RSS feed

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know the difference please
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A synchronized method acquires a lock on the whole object that has the method. Think about two synchronized methods A & B and two threads 1 & 2. If thread 1 enters method A, then thread 2 cannot enter method A or B until thread 1 exits A. That makes sense if both methods modify multiple variables in the object, but may be overkill for simple methods.

When you synchronize a block on some other object you can pick what you synchronize on and how long. I have an object where some methods sync on a counter and some methods sync on a message. I figured out that it's ok for one thread to modify the counter while another modifies the message, but I can't let two threads modify the counter or two threads modify the message.

We had another discussion about how to chose between sync on method and sync a block on an object. Most people preferred to keep the scope of the sync block as small as possible (and no smaller, of course) but I don't think we agreed on better rules to help you decide.

Here's a promising looking article on synchronizing: How the JVM Performs Thread Synchronization. Hope that helps!
[ October 28, 2004: Message edited by: Stan James ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember reading an article about a year ago (on sun.com?) that was related to performance. One of the issues was that while the following two methods are semantically the same:




... the latter one will perform better.
I have tried and tried to search for this article, so if anyone knows what I'm talking about, I'd appreciate a reference.
[ October 28, 2004: Message edited by: Tony Morris ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

another situation where you might want to synchronize on a block rather than a method is if you want to use an unsynchronized method from another class whose source code you don't have access to, but you require to use that method in a synchronized manner in your own code. In this case, since you cannot access the source code of the original method to add the synchronized keyword, you have no choice but to synchronize on a block of code via the object that calls the method you want to use.

DD
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DD, excellent catch! I just had to write synchronized(this) myself because the method was defined in an interface that didn't have synchronized.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!