views:

59

answers:

1

I need to run in parallel multiple threads to perform some tests.

My 'test engine' will have n tests to perform, each one doing k sub-tests. Each test result is stored for a later usage.

So I have n*k processes that can be ran concurrently.

I'm trying to figure how to use the java concurrent tools efficiently.

Right now I have an executor service at test level and n executor service at sub test level.

I create my list of Callables for the test level. Each test callable will then create another list of callables for the subtest level. When invoked a test callable will subsequently invoke all subtest callables

  • test 1
    • subtest a1
    • subtest ...1
    • subtest k1
  • test n
    • subtest a2
    • subtest ...2
    • subtest k2

call sequence:

  • test manager create test 1 callable
    • test1 callable create subtest a1 to k1
    • testn callable create subtest an to kn
  • test manager invoke all test callables
    • test1 callable invoke all subtest a1 to k1
    • testn callable invoke all subtest an to kn

This is working fine, but I have a lot of new treads that are created.

I can not share executor service since I need to call 'shutdown' on the executors.

My idea to fix this problem is to provide the same fixed size thread pool to each executor service.

Do you think it is a good design ? Do I miss something more appropriate/simple for doing this ?

+3  A: 

Use a single fixed thread pool executor. Avoid calling shutdown, if you are doing that you likely have some error.

Here is some pseudo-code that is my best guess at what you want with the little information posted.

main () {
    ArrayList<Future<?>> futures = new ArrayList<Future<?>>();
    ExecutorService exec = Executors.newFixedThreadPool(Runtime.getNumProcessors())
    futures.add(exec.submit(Test1));
    ...
    futures.add(exec.submit(Testn));

    for (Future<?> future:futures) {
       ? result = future.get();
    }
}
Tim Bender
I'm using the invokeAll, but I was missusing it; I understood that it was an asynchronous method and that I need to to call awaitTermination in order to wait for all my task completion (in this case shutdown is mandatory...).Thanks.
Guillaume