如何并行而不是按顺序执行多个模型



我有各种各样的模型,我正在按顺序一个接一个地执行这些模型。我会有大约200个模型。

下面是我的代码,我在其中逐个执行这些模型

Map<String, List<ServerMetrics>> metricsHolder = new HashMap<String, List<ServerMetrics>>();
for (String alias : serverNames) {
    List<ClientsModel> modelMetadata = getModelAttributes();
    List<MachineInfo> machineInfo = getMachineInfo();
    List<ServerMetrics> metricsList = new ArrayList<ServerMetrics>();
    // calling model one by one sequentially
    // is there any way to make this multithreaded?
    // here modelMetadata size might be 100 or 200
    for (ClientsModel modelList : modelMetadata) {
        String modelValue = modelList.getModelValue();
        String modelId = String.valueOf(modelList.getModelId());
        // execute my model here and storing the metrics in the metricsList object
        ServerMetrics metrics = TestUtils.executeModelMetrics(machineInfo, modelValue);
        metrics.setModelId(modelId);
        metricsList.add(metrics);
    }
    metricsHolder.put(alias, dynMetricsList);
}

问题说明:-

有没有什么方法可以让模型执行多线程,然后将结果存储到metricsList对象中?

如果代码中没有数据竞争条件(对未正确同步的同一数据的多线程访问(,则可以使用ExecutorService形式的线程池在线程上运行Callable实现。你可以在Callable中完成工作。

在主线程上,ExecutorService返回一个Future,一旦所有任务都派生出来,您就可以等待它。

线程池的大小(此处设置为10,但您可以更改它(决定了并行运行的数量。您仍然可以执行比线程池大小更多的Callable

    Map<String, List<ServerMetrics>> metricsHolder = new HashMap<String, List<ServerMetrics>>();
    // Size of thread pool set at 10 - can be increased but increasing it 
    // to more than the number of cores on your computer is probably not 
    // useful, as it seems like your task is CPU-bound
    ExecutorService executorService = Executors.newFixedThreadPool(10);
    for (String alias : serverNames) {
        List<ClientsModel> modelMetadata = getModelAttributes();
        List<MachineInfo> machineInfo = getMachineInfo();
        List<Future<ServerMetrics>> metricsFutureList = new ArrayList<Future<ServerMetrics>>();
        // calling model one by one sequentially
        // is there any way to make this multithreaded?
        for (ClientsModel modelList : modelMetadata) {
            final String modelValue = modelList.getModelValue();
            final String modelId = String.valueOf(modelList.getModelId());
            metricsFutureList.add(executorService.submit(new Callable<ServerMetrics>() {
                @Override
                public ServerMetrics call() throws Exception {
                    // execute my model here and storing the metrics in the list
                    ServerMetrics metrics = TestUtils.executeModelMetrics(machineInfo, modelValue);
                    metrics.setModelId(modelId);
                    return metrics;
                }
            }));
        }
        List<ServerMetrics> metricsList = new ArrayList<ServerMetrics>();
        for (Future<ServerMetrics> future : metricsFutureList) {
            metricsList.add(future.get());
        }
        metricsHolder.put(alias, dynMetricsList);
    }

相关内容

  • 没有找到相关文章

最新更新