[concurrency-interest] Design of Thread Safe Iterator Proxy

Remi Forax forax at univ-mlv.fr
Sun Jun 16 19:04:48 EDT 2013

On 06/10/2013 08:37 AM, piyush katariya wrote:
> Hi,
>        so i was in the need of ThreadSafe iterator, so that multiple 
> threads can access over it concurrently without  throwing 
> "ConcurrentModificationException".
> i came with solution attached herewith, but for some reason..multiple 
> threads from thread pool after iterating over, stucks...
> can somebody help me with it..

I can explain why your program deadlock, it's easy.
I will only explain why it deadlock if the client code is:
   while(hasNext()) { it.next(); }
but it can also deadlock is hasNext is called multiple times without 
calling next and vice-versa.

so if the client code is:
   while(hasNext()) { it.next(); }
when hasNext returns false, next is not called because you go out of the 
loop but you still hold the lock,
so the next thread will stop when trying to acquire the lock in hasNext.

BTW, I think there is no way to write a thread-safe proxy iterator if 
remove() is supported.

> Regards,
> Piyush Katariya


More information about the Concurrency-interest mailing list