13

I am posed with the following problem: I need to split work across multiple threads for perfomance reasons, but I am not sure what approach to take.

Firstly, the task I would be supplying should return a value and take a parameter. Additionally, the main method (doing the main bit of work, not static main() ) is already running on separate thread and is invoked periodically. Also, this method must at some point WAIT for all threads to finish and then proceed.

One approach (most obvious to me) is to schedule each job on a separate thread and store results in class vars:

public Object result1, result2;

public void mainMethod() throws InterruptedException {
    final Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            result1 = expensiveMethod("param1");
        }
    });

    final Thread thread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            result2 = expensiveMethod("param2");
        }
    });

    thread1.join();
    thread.join();

    //Do rest of work
}

private Object expensiveMethod(Object param){
    // Do work and return result
}

This is a bit ugly and not ideal, since as I said, mainMethod is invoked many times, and I do not want any race conditions on setting the result variables. Ideally, I would like to make them local variables, but I cannot make them accessible from within the run method, unless they are final, and then I cannot assign values to them...

Another approach I though about doing was this:

public void mainMethod() throws InterruptedException, ExecutionException {
    String obj1, obj2;

    final ExecutorService executorService = Executors.newFixedThreadPool(16);
    final Future<String> res1 = executorService.submit(new Callable<String>() {
        @Override
        public String call() throws Exception {
            return expensiveMethod("param1");
        }
    });
    final Future<String> res2 = executorService.submit(new Callable<String>() {
        @Override
        public String call() throws Exception {
            return expensiveMethod("param2");
        }
    });

    obj1 = res1.get();
    obj2 = res2.get();

}

private String expensiveMethod(String param) {
    // Do work and return result
}

This automatically waits on these two computations from main method and allows me to store the results locally. What to you guys think? Any other approaches?

Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
Bober02
  • 15,034
  • 31
  • 92
  • 178

6 Answers6

16

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 with its actor based concurrency model.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
2

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!)

Kalpak Gadre
  • 6,285
  • 2
  • 24
  • 30
2

Slightly different approach is:

  • create a LinkedBlockingQueue

  • pass it to each task. Tasks can be Threads, or Runnables upon j.u.c.Executor.

  • each task adds its result to the queue

  • the main thread reads results using queue.take() in a loop

This way results are handled as soon as they are computed.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
2

You want to use the CompletionService and keep track of the submitted tasks.
In your loop you then take() and exit the loop when you've got all you tasks completed.
Scale very well is you add more tasks later.

1

I shall add a proposal that is in my eyes more elegant than creating a whole new class for your parametrized Callable. My solution is a method that returns a Callable instance:

Callable<String> expensive(final String param) {
  return new Callable<String>() { public String call() { 
    return expensiveMethod(param);
  }};
}

This even makes client code more palatable:

final Future<String> f1 = executor.submit(expensive("param1"));
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1
private static final ExecutorService threadpool = Executors.newFixedThreadPool(3);

    ArrayList<Future<List<Data>>> futures = new ArrayList<Future<List<Data>>>();
    for (ArrayList<Data> data : map.values()) {
        final Future<List<Data>> future = threadpool.submit(new ValidationTaskExecutor(data));
        futures.add(future);
    }
    List<List<Data>> res = new ArrayList<List<Data>>();
    for (Future<List<Data>> future : futures) {
        allValidRequest.addAll(future.get());

    }
ketan
  • 19,129
  • 42
  • 60
  • 98
steven
  • 11
  • 1