java 在ExecutorService中使用invokeAll获取Future对象时遇到问题

j9per5c4  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(155)

使用invokeAll方法获取Future对象时遇到以下错误。我正在尝试实现ExecutorService以并行调用。
错误〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉〉java.util.concurrent.执行异常:java.lang.不支持的操作异常
代码片段
主要类别:

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

类可调用任务:

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

提前谢谢你!!!

krugob8w

krugob8w1#

实际上,例外情况发生在这里:你不能调用emptymap#put

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

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"
    }

相关问题