Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Iterator Concurrency

I'm trying to loop over a Java iterator concurrently, but am having troubles with the best way to do this.

Here is what I have where I don't try to do anything concurrently.

Long l;    
Iterator<Long> i = getUserIDs();

while (i.hasNext()) {
    l = i.next();

    someObject.doSomething(l);
    anotheObject.doSomething(l);
}

There should be no race conditions between the things I'm doing on the non iterator objects, so I'm not too worried about that. I'd just like to speed up how long it takes to loop through the iterator by not doing it sequentially.

Thanks in advance.

like image 610
Nick Avatar asked Feb 26 '12 17:02

Nick


1 Answers

One solution is to use an executor to parallelise your work.

Simple example:

ExecutorService executor = Executors.newCachedThreadPool();

Iterator<Long> i = getUserIDs();
while (i.hasNext()) {
    final Long l = i.next();

    Runnable task = new Runnable() {
        public void run() {
            someObject.doSomething(l);
            anotheObject.doSomething(l);
        }
    }

    executor.submit(task);
}

executor.shutdown();

This will create a new thread for each item in the iterator, which will then do the work. You can tune how many threads are used by using a different method on the Executors class, or subdivide the work as you see fit (e.g. a different Runnable for each of the method calls).

like image 145
skaffman Avatar answered Oct 06 '22 00:10

skaffman