简体   繁体   中英

Java: design for using many executors services and only few threads

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 ?

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM