Java - splitting work to multiple threads

Firstly, you may want to externalize the creation of ExecutorService from your mainMethod() If this is getting called frequently, you are potentially creating a lot of threads.

Future approach is better as this is exactly what Futures are for. Also, it makes reading code a lot easier.

On a lighter note, although you may have to define your objects as final, you can always have setter methods on the object which can be called no matter your reference is final or not, potentially allowing you to change values of final Objects. (References are final objects are not!)


Your approach with ExecutorService is pretty much the most modern and safe way to do this. It is recommended to extract your Callables to separate class:

public class ExpensiveTask implements Callable<String> {

    private final String param;

    public ExpensiveTask(String param) {
        this.param = param;
    }

    @Override
    public String call() throws Exception {
        return expensiveMethod(param);
    }

}

which will make your code much cleaner:

final ExecutorService executorService = Executors.newFixedThreadPool(16);
final Future<String> res1 = executorService.submit(new ExpensiveTask("param1"));
final Future<String> res2 = executorService.submit(new ExpensiveTask("param2"));
String obj1 = res1.get();
String obj2 = res2.get();

A few notes:

  • 16 threads are too much if you only want to process two tasks simultaneously - or maybe you want to reuse that pool from several client threads?

  • remember to close the pool

  • use lightweight ExecutorCompletionService to wait for the first task that finished, not necessarily for the first one that was submitted.

If you need a completely different design idea, check out akka with its actor based concurrency model.