File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what to use in place of Vector

 
Saurabh Saha
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Thanks,
Vivek Mishra
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check the API docs, there's a synchronization wrapper for ArrayList that will provide synchronization transparently.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic