Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronization and Performance

 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method that I was testing the performance of that can be called about 500,000 times a second from within my testing servlet. If the code is synchronized it runs slightly slower, say 1.5 seconds(I don't recall the percentage). However, I wanted to see how synchronization affected performance when I had 5 simultaneous servlets accessing the same method (each servlet gets the same instance variable).

Running the unsynchronized code on 5 simultaneous servlets on a windows box took about 5 seconds. I would have expected hte unsynchronized code not to not execute as if each servlet was performing sequentially.

The synchronized code slowed down to about 30 seconds for the 5 pages too complete. I am surprised that the worse case for the multithreaded test wouldn't just be 5 threads times 1.5 seconds = 9 seconds.

Can anyone shed any light on this?
 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Speaking in general terms, you can't really draw exact linear metrics from the behavior of a web application in terms of response time and scaling. The application server will be doing all sorts of throttling and balancing to make sure it is as responsive with 100 clients as with 5. You can't just say that if 5 clients took 9 seconds, then your application takes 1.5 seconds a page. I'll bet the cpu on your server was nowhere near 100% use while that app ran and that is by design.
Furthermore, when you synchronize your code, depending on where you synchronize it, you may be forcing the app server to serialize activities which it can do in parallel with unsynchronized code, like IO. This, with the fact that serializing code has the overhead of locking objects and determining which object gets the lock once released, should account for the non-linear performance of your synchronized code.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a nice article on the topic http://www-128.ibm.com/developerworks/java/library/j-jtp10264/
 
John Dunn
slicker
Ranch Hand
Posts: 1108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great article, Steve!! Thanks. Another reason to get to JDK5.0 Asap.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Dunn:
Great article, Steve!! Thanks. Another reason to get to JDK5.0 Asap.


Most of the functionality is also available for 1.4: http://dcl.mathcs.emory.edu/util/backport-util-concurrent/index.php
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a nice article on improvements to the collections for concurrency in JDK 1.5. http://www-128.ibm.com/developerworks/library/j-jtp07233.html

Also if you need better concurrency control in older versions of the jdk check out doug leas threading classes. He also has a nice book on the topic. The author of the articles mentioned above (Brian Goetz) is coming out with a book on java concurrency/threading too.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added this info on concurrency to the java performance faq mentioned below
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic