• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronization  RSS feed

 
Kirk Pepperdine
Author
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just been looking at a set of slides that suggest that
public synchronized void method() {....
out performs
public void method() {
synchronize(this) {
............
My understanding is that each would get the monitor for the object, the later would just do it a bit later.
What might account for the timing difference?
Kirk
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On some VMs, not all, using the synchronized method modifier can prove slightly more efficient than using a synchronized block when synchronizing over an entire method. This is probably due to the fact that using the synchronized method modifier doesn't generate any additional bytecode and all the synchronization stuff (getting and releasing the monitor, handling exceptions) is done more efficiently in the VM. Using the synchronized method modifier simply has the effect of setting the ACC_SYNCHRONIZED flag on inside the .class file for that method. Usage of the synchronized block, on the other hand, results in the generation of all the bytecode to do all the locking and unlocking and handling exceptions.
If an entire method needs to be synchronized, you will get a smaller .class file by using the synchronized method modifier. On some VMs, you might also get slightly faster execution speed.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
 
Kirk Pepperdine
Author
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
Great explinatoin. Looking at the accomaning graph, I noted that the performance difference is minimal. It also seems that your results would be very sensitive to the type of application that was being supported. I was always a proponent of using
Object o = new Object();
public void method() {
....
synchronize(o) {
.....
}
......
}
in an attempt to limit the size of the critial section. Maybe this methodology needs to be questioned in light of your comments.
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kirk Pepperdine:
I was always a proponent of using
Object o = new Object();
public void method() {
....
synchronize(o) {
.....
}
......
}
in an attempt to limit the size of the critial section. Maybe this methodology needs to be questioned in light of your comments.

Your methodology is fine and is the preferred way to do it. However, in your first post both methods synchronized on this, and in the above example, you are synchronizing on an instance of Object, whereas if you added the synchronized method modifier to the method, it would synchronize on this. Therefore, removing the sync block in the above code and replacing it with a synchronized method would make the method lock on a different object.
I would always prefer to limit the size of a synchonrized block in an attempt to increase the concurrency of the code. I would *only* convert a synchronized(this) block to a synchronized method:
- if the block encompassed the entire method, thus the two forms are functionally identical. As discussed, removing the block produces less bytecode (this may or may not be important) and might produce slightly faster code (this may or may not be important). It also has the effect of being more obvious that the method is synchronized by looking at it in documentation (probably the best reason to do it).
- if I could afford the loss of concurrency for a proven performance gain. This will be highly dependent on the VM.
- if I could affort the loss of concurrency for space restrictions. Maybe in an embedded application where you were very tight on space and had a lot of synchronized blocks and had to squeeze every last bit of baggage from your .class files.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!