This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Vector iterator threadsafe? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Vector iterator threadsafe?" Watch "Vector iterator threadsafe?" New topic
Author

Vector iterator threadsafe?

Rudolfoo Schmidt
Greenhorn

Joined: Oct 06, 2005
Posts: 5
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

Joined: Oct 05, 2001
Posts: 1170

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

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Sep 28, 2004
Posts: 16692
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Oct 05, 2001
Posts: 1170

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

Joined: Jul 15, 2003
Posts: 1078
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?
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Vector iterator threadsafe?
 
Similar Threads
Synchronising Vector
Collection/Iterator
Iterators from Vector are fail-safe?
static method - thread safety
Vectors and Synchronization