One solution is to use an artist to parallelize your work.
A 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 element in the iterator, which will then do the job. You can configure how many threads are used with another method in the Executors
class, or split the work as you like (for example, another Runnable
for each method call).
source share