wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes ArrayList or Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ArrayList or Vector" Watch "ArrayList or Vector" New topic
Author

ArrayList or Vector

Faisal Shahzad
Greenhorn

Joined: Dec 07, 2013
Posts: 5

I needs to develop a threading based application. The application will perform complex real time calculations, which includes about 80% read operations and about 20% write operations.
The application should have the ability to:
Efficiently use Memory resource
Perform fast operations
Maintain insertion order of elements and
Run in thread-safe mode

which of the following java collection will you prefer?

ArrayList
OR
Vector
Justify your answer in each case.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Neither. My first option would be CopyOnWriteArrayList because thread safe access where reads out number writes is what it is optimized for

There are cases where it is not appropriate so I would measure and if I saw a problem them I might search for a different implementation that better matches the use case.


Steve
Roger Sterling
Ranch Hand

Joined: Apr 06, 2012
Posts: 426

Vector is advertised thread-safe, but has some issues. ArrayList is advertised as un-safe and requires the application coder to add thread safety if needed. I tend to use Vector more than not.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
Steve Luke wrote:Neither. My first option would be CopyOnWriteArrayList because thread safe access where reads out number writes is what it is optimized for

Well, maybe. According to the docs, it's optimized for cases where reads vastly outnumber writes. "Vastly" is not defined, but I suspect 20% writes 80% reads might not be a big enough difference to justify CopyOnWriteArrayList.

But, as you say:
Steve Luke wrote:There are cases where it is not appropriate so I would measure and if I saw a problem them I might search for a different implementation that better matches the use case.

Yes, definitely measure.

For an alternative, if needed, I would definitely prefer using ArrayList (or possibly LinkedList or ArrayDeque or other implementation) over Vector. Add synchronization explicitly where you need it. Do not rely on the built-in synchronization of Vector; it is usually too low-level to be truly useful, and misleads many people into thinking they are thread-safe when they are not. You almost always need additional synchronization anyway, so making all your synchronization explicit in your code seems clearer and safer, to me.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Just a simple example to follow up on Mike's statement about how the unwary programmer can be misled by the "thread-safe" label:



The beginner might think that "thread safety" of the list would make that acceptable code to ensure that the list contained only one instance of the value. But it isn't. You need that additional synchronization which Mike mentioned.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

Given the choices, limited to either Vector or ArrayList, I'd definitely go with ArrayList. Vector is widely considered obsolete, though it is not officially flagged as deprecated. Also the same thread-safety promises made by Vector can trivially be applied to ArrayList simply by wrapping it via Collections#synchronizedList(). The most compelling reason, though, has already been stated by Mike and Paul, which is that the thread-safety guarantees made by Vector are often insufficient or cause too much contention, or both. So definitely ArrayList.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
 
Don't get me started about those stupid light bulbs.
 
subject: ArrayList or Vector