I have a list of approximately a thousand Java objects and am iterating a List
container to process them, the same processing for every object. This sequential approach is taking a lot of time for processing, so I want to attempt to speed it up with parallel processing. I checked Java executor frameworks but got stuck.
I thought of one approach to implement my requirement. I want to specify some minimum fixed number of objects to be processed by each thread so that each does its processing in a quick manner. How can I achieve this? Should I use another approach?
For example:
List<Object> objects = new List<Object>();
for (Object object : objects) {
// Doing some common operation for all Objects
}
You can use a ThreadPoolExecutor
, it will take care of load balance. Tasks will be distributed on different threads.
Here is an example:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
public static void main(String[] args) {
// Fixed thread number
ExecutorService service = Executors.newFixedThreadPool(10);
// Or un fixed thread number
// The number of threads will increase with tasks
// ExecutorService service = Executors.newCachedThreadPool(10);
List<Object> objects = new ArrayList<>();
for (Object o : objects) {
service.execute(new MyTask(o));
}
// shutdown
// this will get blocked until all task finish
service.shutdown();
try {
service.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static class MyTask implements Runnable {
Object target;
public MyTask(Object target) {
this.target = target;
}
@Override
public void run() {
// business logic at here
}
}
}