File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes what to use in place of Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what to use in place of Vector" Watch "what to use in place of Vector" New topic

what to use in place of Vector

Saurabh Saha
Ranch Hand

Joined: Dec 08, 2004
Posts: 52
Hi All,
we use ArrayList for new code but if i want to use Synchronization then what to do. I know that now a days we not use Vector.
Please guide me!

Vivek Mishra
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
check the API docs, there's a synchronization wrapper for ArrayList that will provide synchronization transparently.

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
check the API docs

Specifically, the Collections class.

However I think that in many cases where you do need synchronization somewhere in your code, the wrapper doesn't provide the sync in the right place. Consider:

Now let's say list is shared by multiple threads, so you attempt to protect it by wrapping it with Collections.synchronizedList() before passing it to this method. Does that make this code thread-safe? No. The sync wrapper synchronized each individual method call - but it doesn't protect you from other threads slicing in between synchronized method calls. In the above code, what if another thread removes an element from the list after size() is called, but before get(i) is called? You could have a runtime exception as the index is out of bounds. To protect you code propertly here, you'd need to sync at a higher level:

In general, I think that the Collections.synchronizedXXX() methods are of very limited use, and often give a false sense of security. Much like Vector and Hashmap do. If you need synchronization, you are usually better off putting it in yourself, I think.
[ January 23, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
It is sorta covered in the JavaRanch Style Guide.
subject: what to use in place of Vector
It's not a secret anymore!