Concurrency problem in org.jivesoftware.wildfire.user.UserCollection

Problem:[/b]

The following code fails with IndexOutOfBoundsException:

Collection(allUsers);

/code

The following code passes, but yields corrupted data - please check the assertions in the end of the snippet.

Collection it=users.iterator(); i.hasNext():wink: {

de.add("Her " + it.next().getName());

}

}};

japanizer.start();

germanizer.start();

assert ja.size()+de.size()==users.size() : “This one passes.”;

assert ja.size()==users.size() : “This one will fail.”;

assert de.size()==users.size() : “This one will fail.”;

/code

Reason:[/b]

The currentIndex[/i] and nextElement[/i] fields belong in UserIterato[/i]r and not in UserCollection[/i].

We have 2 issues here:

The iterators share state which violates the iterator contract and fails when we hava concurrent access[/b] (not necessarily multi threaded, there are certain algorithms which use slow and fast iterator over the same collection).

Even if we don’'t have concurrent access, once we finish iterating over the collection there os no way we can reset currentIndex[/i] to point to the beginning of the data again.

Fix:[/b]

Move currentIndex[/i] and nextElement[/i] fields to UserIterator[/i]

Thanks for the great bug report and fix! I filed this as JM-554 and am checking in a fix now.

Regards,

Matt