I am making a program that distributes a task. I have arraylist of communicator objects like so:
ArrayList<Workers>
I am working my way through a file, dividing it into fixed size chunks and dispatching to the various Workers. I am using an iterator to pass the chunks to the workers evenly. Usually there are more chunks than workers so i need to loop around and around my workers. How do i do this, my current solution uses an iterator like so.
private Worker getNextWorker() {
if (workerIterator == null)
workerIterator = workers.iterator();
if (!workerIterator.hasNext())
workerIterator = workers.iterator();
return workerIterator.next();
}
I synchronised the method as well as the methods modify the arraylist however this doesnt make it safe as another thread can come in and modify the collection between iterator calls. Therefore i syncronised the entire file splitting process to make it one large atomic statement.
1) Have i missed anything?
2) Is there another, perhaps better way i get can this loop around functionality.