This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Vector iterator threadsafe?

 
Rudolfoo Schmidt
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a vector storing 200 objects. I want several threads to "consume" these objects, which means that for every request one object should be returned and never should be picked again.

For this I use the vectors iterator and let each thread call the next() method (which threads concurrently do). Do I have to be aware of any synchronisation issues?

Thx, Rudolf
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use ArrayList instead of vector. Vector is an old implementation that should really be deprecated. Not a big deal though.

Seeing the implementation, i dont think vector iterator is thread safe. ArrayList is not either. I dont think iterators can be made thread safe without work. They do however throw exceptions when something unsafe happens, or they try to.
[ October 12, 2005: Message edited by: Mr. C Lamont Gilbert ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mr. C Lamont Gilbert:
Use ArrayList instead of vector. Vector is an old implementation that should really be deprecated. Not a big deal though.

Seeing the implementation, i dont think vector iterator is thread safe. ArrayList is not either. I dont think iterators can be made thread safe without work. They do however throw exceptions when something unsafe happens, or they try to.

[ October 12, 2005: Message edited by: Mr. C Lamont Gilbert ]


Wasn't Vector rewritten to be part of Collections? *concerned* I've been using Vectors a lot.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really understand what exactly you're trying to do, but couldn't you use the Iterator and have it be thread-safe if you wrapped it? Something like:


[ October 12, 2005: Message edited by: Ken Blair ]
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wasn't Vector rewritten to be part of Collections? *concerned* I've been using Vectors a lot.


Yes... it is perfectly fine to use Vector. Nothing to be concerned about.

To answer the original question... The iterator will fail if any modification of the collection is done outside of the iterator. You can only use the iterator to change values, you can't use the collection, or any other iterator at the same time.

However, it can be considered "thread-safe" as that is how it was designed to work...

Henry
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


Yes... it is perfectly fine to use Vector. Nothing to be concerned about.

To answer the original question... The iterator will fail if any modification of the collection is done outside of the iterator. You can only use the iterator to change values, you can't use the collection, or any other iterator at the same time.

However, it can be considered "thread-safe" as that is how it was designed to work...

Henry


Yes, but is using iterator.next() thread-safe? If multiple threads are calling iterator.next() on the same Iterator, that's not modifying the Collection outside the Iterator, yet I don't see it mentioned in the API whether or not this is thread-safe.

Futhermore, rather than asking in a new Thread()...HAR HAR HAR...I seriously do want to know if the code I posted will guarantee that such a thing was thread-safe by making it impossible to access the Vector or Iterator being used. This is of course assuming no methods were added in the future that gave access to those. The only references to the Vector and Iterator would be held within the class and inaccessible to the public (or within the actual Iterator/Vector if they hold references to each other), thus it'd be impossible for another Thread to modify them right?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector is passe Its synchronization is often redundant and unnecessary or useless and wasteful. It makes more sense to have a Collections.synchronizedXXX method to create such an instance if you actually find the need. If you use vector in this particular case you will be wasting synchronization and accomplishing nothing.

Now to the point. Vector is not thread safe in this usage. The iterators do not guarantee to throw the exception during concurrent modification; Even if you did want to consider throwing an exception as making your code thread safe.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now to the point. Vector is not thread safe in this usage. The iterators do not guarantee to throw the exception during concurrent modification; Even if you did want to consider throwing an exception as making your code thread safe.


I assume that was in reply to the notion that it's thread-safe because it will throw a ConcurrentModificationException if it's modified outside of the Iterator. The API does say it's a "best effort" situation and can't be relied upon.

So, would what I posted be considered thread-safe?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!