[concurrency-interest] Design of Thread Safe Iterator Proxy

corporate.piyush at gmail.com corporate.piyush at gmail.com
Sun Jun 23 03:20:25 EDT 2013

sorry for late reply...

@Remi & @Yuvit

thanks for your time.
I know that concrete solution to this problem is not possible as hasNext() 
and next() are two different methods.

But we can at least think of Safe Iterator proxy , assuming the following

1. every thread which calls hasNext() is expected to call next() , again in 
2. once a particular thread calls hasNext(), a lock will be active until the 
same thread calls next() and retrieves the element from underlying iterator
3. during the execution of step 2, all other threads have to wait for their 

Piyush Katariya

-----Original Message----- 
From: Remi Forax
Sent: Monday, June 17, 2013 4:34 AM
To: concurrency-interest at cs.oswego.edu
Subject: Re: [concurrency-interest] Design of Thread Safe Iterator Proxy

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


Concurrency-interest mailing list
Concurrency-interest at cs.oswego.edu
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: SafeIterator.txt
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20130623/376d86ea/attachment.txt>

More information about the Concurrency-interest mailing list