Home > Mobile >  Facing issue while using invokeAll to fetch Future object in ExecutorService
Facing issue while using invokeAll to fetch Future object in ExecutorService

Time:01-06

Facing below error while fetching Future object by using invokeAll Method. Iam trying to implement ExecutorService to call parallelly.

ERROR>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException

Code Snippet

Main 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:

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!!!

CodePudding user response:

In fact the exception happens here:you can't call 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:

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