I've a piece of java code which constructs an object from xml and takes some nanoseconds to a millisecond depending on object size. Sometimes I've to call that method 1-2 times, sometimes 70-80 times in loop to construct a list of objects.
I tried constructing the objects in parallel, but sometimes it's taking double time than sequential and half the other times. Now my question is are there any guidelines or performance comparison metrics to guide when should multitasking be used and when it's just an overkill?
Sample code that I'm using is:
List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
for (final Integer object : list) {
Callable<Integer> c = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return test.m1(object);
}
};
tasks.add(c);
}
List<Future<Integer>> results = EXEC.invokeAll(tasks);
for (Future<Integer> fr : results) {
fr.get();
}