• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

vetor thread safe?

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

If I use a vector to store some objects much I synchronize access or is it thread safe?

Thanks
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is thread-safe.
 
Rancher
Posts: 3787
49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is, technically, in the sense that each method individually is thread-safe. However it's difficult to accomplish anything useful with this class without some additional synchronization. For example when iterating, you need to synchronize, as shown here. Yes, that discussion is for Collections.synchronizedList(), but Vector works the same way. The basic problem is that while each individual method call is synchronized, nothing prevents other threads from inserting other method calls in between two calls that really need to go together. If you've just made a call to iterator.hasNext() and it returned true, can you safely call iterator.next()? Not without extra synchronization - because how do you know that some other thread didn't remove() or clear() the next item, immediately after you called hasNext()? In general, you don't. There are other examples where Vector and synchronizedList() usage require additional synchronization, but iteration is the most common.

My own opinion is that the synchronization in Vector and Collections.synchronizedList() is close to useless, and dangerously misleading. You're usually better off synchronizing on the list externally, using synchronized blocks. Or writing the code in a way that no synchronization is required at all.
 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your detailed explanation. I will use locks.
 
Happily living in the valley of the dried frogs with a few tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic