• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what to use in place of Vector

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check the API docs, there's a synchronization wrapper for ArrayList that will provide synchronization transparently.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic