简体   繁体   English

在 ExecutorService 中使用 invokeAll 获取 Future object 时遇到问题

[英]Facing issue while using invokeAll to fetch Future object in ExecutorService

Facing below error while fetching Future object by using invokeAll Method.使用invokeAll方法获取Future object 时遇到以下错误。 Iam trying to implement ExecutorService to call parallelly.我正在尝试实现ExecutorService以并行调用。

ERROR>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException错误>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>java.util.concurrent。执行异常:java.lang.UnsupportedOperationException

Code Snippet代码片段

Main Class:总机 Class:

ExecutorService exService = Executors.newFixedThreadPool(1);
List<CallableTask> listOfTask = new ArrayList<CallableTask>();
Map<String, String> param1= params;
param1.put("catCode", "XX278293##X");
listOfTask.add(new CallableTask(param1));

List<Future<Map<String,String>>> resultSet =  exService.invokeAll(listOfTask);
for(Future<Map<String,String>> result : resultSet){
    if(result.isDone()){
        Map<String,String> response = result.get(); <<----------Facing Error in this line
    }           
}
exService.shutdown();

Class CallableTask: Class 可调用任务:

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.Callable;

class CallableTask implements Callable<Map<String, String>>{
    private String name;
    private Map<String, String> params;
    public CallableTask(Map<String, String> params){    
        this.name=params.get("catCode");
        this.params =params;
    }
    @Override
    public Map<String, String> call() throws Exception {
       boolean data = false;
        //REST CALL
    Map<String, String> resultData = Collections.emptyMap();
        resultData.put("result", name+data);    
        //Getting response upto here
        return resultData;
    }
}   

thank you in advance!!!先感谢您!!!

In fact the exception happens here:you can't call emptymap#put :事实上异常发生在这里:你不能调用emptymap#put

Map<String, String> resultData = Collections.emptyMap();
resultData.put("result", name+data);

Future#get just reports the exception that occurs during the execution of CallableTask : Future#get只是报告在执行CallableTask期间发生的异常:

   FutureTask#report:

   private V report(int s) throws ExecutionException {
        Object x = outcome;  --Here: outcome is "java.lang.UnsupportedOperationException".
        if (s == NORMAL)
            return (V)x;
        if (s >= CANCELLED)
            throw new CancellationException();
        throw new ExecutionException((Throwable)x); --Status of this task is "EXCEPTIONAL"
    }

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

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