• Post Reply Bookmark Topic Watch Topic
  • New Topic

Performance of Synchronized ArrayList & Vector

 
Md Fizal
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • 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?
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronization has a performance cost, so is not recommended unless your app is multithreaded & requires it.
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!