• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronization and Performance

 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found a nice article on the topic http://www-128.ibm.com/developerworks/java/library/j-jtp10264/
 
slicker
Posts: 1108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great article, Steve!! Thanks. Another reason to get to JDK5.0 Asap.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added this info on concurrency to the java performance faq mentioned below
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic