commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jack, Paul" <>
Subject RE: [SUBMIT][Collections] Buffer stuff from Avalon
Date Fri, 28 Jun 2002 15:50:40 GMT
> no, that's the way that Sun decided to implement 
> Collections.synchronizedFoo, so you're right.  I was just 
> saying that I 
> thought Sun could have implemented the iterator so that 
> wasn't required 
> for a multi-thread safe iterator.

Well, yes, they could have synchronized the iterator methods
with the same monitor they used for the collection wrapper, 
the resulting thread-safe iterators would be kind of useless.

First, you couldn't use the traditional form:

  Iterator i = c.iterator();
  while (i.hasNext()) process(;

You couldn't use that because there wouldn't be any guarantee
that some other thread hasn't invoked c.clear().  Meaning that
you couldn't guarantee that a true hasNext() result would actually
result in an object from next().  So you'd have to write your 
loops like this:

  Iterator i = c.iterator();
  try {
      while (true) process(;
  } catch (NoSuchElementException e) {

But even that doesn't fully do what you want, because if any other
thread modifies the collection while you're iterating through the
elements, then you get a ConcurrentModificationException:

  Iterator i = c.iterator();
  try {
      while (true) process(;
  } catch (NoSuchElementException e) {
  } catch (ConcurrentModificationException e) {

Of course, if you get the ConcurrentModificationException, it probably
means your iteration was aborted prematurely, and you've have no way 
of detecting which elements were processed and which ones weren't.

What I'm getting at is, even if Sun provided synchronized iterators,
you'd still probably want to use them the same way:

   // block out other threads from making mods
   synchronized (c) {
       Iterator i = c.iterator();
       while (i.hasNext()) process(;

Having said all that, I removed the synchronized iterator() method
from BufferUtils.synchronizedBuffer().  I also renamed BoundedFifo
and UnboundedFifo to BoundedFifoBuffer and UnboundedFifoBuffer per
Stephen's recommendations.

The website's been updated with the changes:


To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message