Fail fast Iterator
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Friday, March 12, 2004 04:46 AM

Hi, I know Iterator and ListIterator in Java are fail-fast Iterator .I need to append to a collection from one thread and read from that collection in another thread. I know this can be done only using the Iterator's add and remove method else it throws ConcurrentModificationException . Now my problem is that I get a ListIterator on the Vector and pass it to both threads. In one thread I add to the collection using ListIterator.add() and in the other thread use something like While (ListIterator.hasNext()) { ListIterator.next(); } Problem is that ListIterator.hasNext() returns me false as the element is added before the element returned by hasNext. "The element is inserted immediately be   More>>

Hi,
I know Iterator and ListIterator in Java are fail-fast Iterator .I need to append to a collection from one thread and read from that collection in another thread. I know this can be done only using the Iterator's add and remove method else it throws ConcurrentModificationException .
Now my problem is that I get a ListIterator on the Vector and pass it to both threads. In one thread I add to the collection using ListIterator.add() and in the other thread use something like
While (ListIterator.hasNext())
{
ListIterator.next();
}

Problem is that ListIterator.hasNext() returns me false as the element is added before the element returned by hasNext.

"The element is inserted immediately before the next element that would be returned by next, if any, and after the next element that would be returned by previous, if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element. (This call increases by one the value that would be returned by a call to nextIndex or previousIndex.) " Is the documentation for add.

I dont want to traverse the list in the reverse order. Any pointers on what to use .

Thanks
Aparna

   <<Less

Re: Fail fast Iterator

Posted By:   Bradford_Maxwell  
Posted On:   Monday, March 29, 2004 08:23 AM


You may not be using the right tool for the job. Are you trying to construct a FIFO cache for i/o? Are you trying to create list that has some persistance?


one way you could do this would be as follows:


on the read thread



String readString = null;
While(true){
if (null == (readString = Listiteartor.next())){
if (null != (readString= Listiterator.previous())){
if (previousString.equals(readString){
continue;
}else{
// do whatever you need to with readString
previousString = readString;
continue;
}
}
}
}


This is still somewhat vulnerable to timing - for example if the writing thread should write twice between reads then the first would be lost since we aren't looking any further back than the immediately previous one. Replacing the if's with while's might solve that problem.

This will attempt to construct a cache. FIFO behavior is not guaranteed tho since if there is a double insertion it will be handled FILO.


If you want to use this to construct an insert-ordered list then you will be sabatoging yourself everytime you issue the previous method call since it will put your cursor in the wrong place for the next write.

About | Sitemap | Contact