dcsimg
The "fail-fast" nature of iterator() method
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Harsh_Soni
Posted On:   Saturday, June 21, 2003 01:28 PM

Hi,




Sun's javadoc for Vector mentions that its iterator() method is fail-fast, i.e., an exception will be thrown if the vector is structurally modified after its iterator is created. But this condition should never arise because Vector is synchronized. No other thread will be able to access the vector when a thread is already using it. The only other way a vector can be modified after an iterator is created is by using the the methods defined on the Vector class itself, such as remove() . Is safety from this case the only reason the iterator() method is called fail-fast?

Re: The "fail-fast" nature of iterator() method

Posted By:   Nick_Maiorano  
Posted On:   Sunday, June 22, 2003 04:23 PM

Harsh,



Even if access to vector methods are synchronized, access to multiple method by multiple threads at the same time is not synchronized. In other words, the synchronization only applies at the method level - not for a series of method invocations on the vector. For example, you could have thread_1 create an iterator, thread_2 add/remove an element, and thread_1 will fail-fast when it tries to access the next element thru the iterator.next() method.



Also, thread_1 can create an iterator, add/remove an element from the vector, then try to iterate over the next element. This too will fail-fast. You don't need mutiple threads to do damage.

About | Sitemap | Contact