• 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

Synchronized

 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the synchronized keyword any impact on performance?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the very least, it compiles to a pair of bytecodes: monitorenter and monitorexit. It used to be that the implementations of these were rather expensive; that's much less true today, especially when the current thread already hold the lock on the given object.
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Theoretically yes, as the threads racing for a lock are paused. Practically the syncronized blocks have been very optimized and now I can say that there are no big performance penalties.

./pope
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are using WAS 5.0, it has JVM 1.3.1, it's running on iSeries. Is it also optimized or are these optimizations in the higher version of JVM?
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Ulicny:
We are using WAS 5.0, it has JVM 1.3.1, it's running on iSeries. Is it also optimized or are these optimizations in the higher version of JVM?



I think they are already in place. A big improvement was brought also in 1.4 (you can hardly see 'em ).

./pope
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Ulicny:
Does the synchronized keyword any impact on performance?



Yes... we actually spend a whole chapter on performance. And a large part of it is on synchronization.

Basically, there are two impacts on performance due to synchronization. First, is the cost to execute the synchronization primatives. And second, the blocking of the thread because of the synchronization. To improve the second, you have to change your algorithm -- which is the discussion topic of the free online chapter. (http://www.oreilly.com/catalog/jthreads3/)

To improve the first, you have to completely remove the synchronization... but as discussed in the performance chapter, it is not that large of a problem. Simply, if the lock is uncontended, it is in the order of nanoseconds. If the lock is contended, it is in the order of milliseconds. Which depending on the size of the synchronization block, can be a really insignificant.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
At the very least, it compiles to a pair of bytecodes: monitorenter and monitorexit. It used to be that the implementations of these were rather expensive; that's much less true today, especially when the current thread already hold the lock on the given object.



I agree... synchronization costs in terms of time to acquire and release locks has been greatly improved in recent JVMs.

Henry
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its not the keyword that has the impact but the whole design of a thread safe program that has the impact. Synchronized keyword is just one part of that.

I would add that you do not synchronize because you want to, you synchronized because you have to. Therefore, the question is moot.
[ October 29, 2004: Message edited by: CL Gilbert ]
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed. We were not talking about a keyword impact on performance - as for the moment I do not see one doing this. People seem to remember the first discussions on synchronization from the time when doing it increased the time by 8 (or so). We were trying to present the now-a-days facts of the JVMs

./pope
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, this "To synchronize or not to synchronize" article may help you.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic