• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Performance of Synchronized ArrayList & Vector

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I learned from this book 'Java Performance Tuning' by 'Jack Shirazi', that Vector is faster than a 'synchronized' ArrayList. Any comments on why this is so?
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Synchronization has a performance cost, so is not recommended unless your app is multithreaded & requires it.
 
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
I'm just taking a stab at this, but looking at the source for java.util.Collections, it appears that synchronizedList() just wraps the unsynchronized list with a synchronized List implementation. This would mean that every method call to the synchronized list would involve another method call to the underlaying unsynchronized list. That's additional overhead, but I doubt that you'd see much of an impact in any real-world application.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The performance cost of synchronization is often overstated. An important issue is: is there contention for the lock from other threads? A more important issue is that code that relies on the internal synchronization of either Vector or synchronizedList() is usually defective anyway, and requires additional external synchronization to make it safe. (It's rare that you can do anything useful with a List that doesn't require at least two method calls, and you need to prevent other threads from changing the data between calls.) Once you put in external synchronization, the internal synchronization becomes pointless. Consequently, between Vector and synchronizedList(), my answer is: who cares?

In comparison, a Hashtable or synchronizedMap() is sometimes useful, because put() and get() can be useful as atomic operations without necessarily requiring other method calls. Last I checked, it seems Hastable could be slightly faster than synchonizedMap(), for the reasons Joe just described. But the effect is usually negligible, and I usually would prefer to use the more flexible newer classes, just on general principle.
 
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
The cost of synchronization is not relevant. You synchronize when you must. It has nothing to do with what you want. If you don't require synchronization, do not use it.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic