简体   繁体   English

Java:设计用于使用许多执行程序服务和仅几个线程

[英]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. 我的“测试引擎”将执行n个测试,每个测试执行k个子测试。 Each test result is stored for a later usage. 存储每个测试结果以供以后使用。

So I have n*k processes that can be ran concurrently. 因此,我有可以同时运行的n * k个进程。

I'm trying to figure how to use the java concurrent tools efficiently. 我试图弄清楚如何有效地使用Java并发工具。

Right now I have an executor service at test level and n executor service at sub test level. 现在,我在测试级别有一个执行程序服务,在子测试级别有n个执行程序服务。

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 测试1
    • subtest a1 子测试a1
    • subtest ...1 子测验... 1
    • subtest k1 子测试k1
  • test n 测试n
    • subtest a2 子测验a2
    • subtest ...2 子测验... 2
    • subtest k2 子测验k2

call sequence: 通话顺序:

  • test manager create test 1 callable 测试经理创建可调用的测试1
    • test1 callable create subtest a1 to k1 test1可调用的创建子测试a1到k1
    • testn callable create subtest an to kn testn可调用创建子测试an到kn
  • test manager invoke all test callables 测试经理调用所有测试可调用项
    • test1 callable invoke all subtest a1 to k1 可调用的test1调用所有子测试a1到k1
    • testn callable invoke all subtest an to kn testn可调用调用所有子测试an到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. 避免调用shutdown,否则可能会出错。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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