Suppose I have the following class:
public class Foo {
private List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5);
private Iterator<Integer> iterator = Iterators.cycle(list);
public void bar(){
Integer value = iterator.next();
doSomethingWithAnInteger(value);
}
}
If an instance of Foo is acessed simultaneously by two threads, I need that each thread gets a different value from iterator.next()
. Does the bar()
method have to be made synchronized? Or is iterator.next()
guaranteed to be thread-safe?
In this example, I am using an ArrayList as the underlying Iterable. Does the thread-safety of the cyclic iterator depend on the specific iterable implementation?
Thank you.