Closed GoogleCodeExporter closed 9 years ago
What's your use case?
Generally, you have to synchronize around an entire iteration, not just
individual calls to hasNext() or next(). For that, a "synchronized wrapper"
can't possibly help -- there's no alternative but to do the synchronization
yourself.
Original comment by lowas...@google.com
on 5 Oct 2012 at 6:05
In my use case, I vend port numbers to tests via an Iterators.cycle(ports).
Thus hasNext always succeeds and I want to synchronize calls to next(). One
can imagine other variants of this, for example vending sequence numbers,
temporary file names, etc. One could use an AtomicInteger for some of these
although that exposes additional mutators.
Original comment by g...@maginatics.com
on 5 Oct 2012 at 6:17
These almost sound like a BlockingQueue of some kind would be more appropriate
than an Iterator, I guess?
Original comment by lowas...@google.com
on 5 Oct 2012 at 6:30
Since you want to hide mutators and have no need for #hasNext and #remove, I'd
say a Supplier is more suitable. It's easy to create a synchronized supplier
which delegates #get calls to some underlying iterator.
Original comment by stephan...@gmail.com
on 5 Oct 2012 at 8:19
Stephan, thank you for suggesting Supplier -- this is a better match for what I
want. Also Suppliers has a synchronized wrapper.
Original comment by g...@maginatics.com
on 6 Oct 2012 at 12:07
Marking as WorkingAsIntended.
Original comment by lowas...@google.com
on 11 Oct 2012 at 6:53
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:13
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:08
Original issue reported on code.google.com by
g...@maginatics.com
on 5 Oct 2012 at 6:02