-2

Callable's call method is not returning list. Also when a debugger point kept at line 'return strList;' in call(), its not coming there at any point of time.

Could someone help, where its getting wrong.

Thanks in advance !

  private static void getThreadNameList() {
        List<String> strList=null;
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        Future<List<String>> future = executorService.submit(new MyCallables());
        try {
            strList = future.get();
        } catch (InterruptedException | ExecutionException e) { }
        for(String s:strList) {
            System.out.println(s);
        }
    }

class MyCallables implements Callable<List<String>>{

    List<String> strList=null;

    @Override
    public List<String> call() throws Exception {   
        for(int i=0;i<=10;i++) {
            strList.add(Thread.currentThread().getName());      
        }
        return strList;
    }
}
Noorus Khan
  • 1,342
  • 3
  • 15
  • 33
  • It seems the strList is not initialized and also it is not good approach to catch (InterruptedException | ExecutionException e) { } eat the exception.Please print the stacktrace and post the error – user06062019 Jul 02 '19 at 10:29

1 Answers1

3

You need to initiate your List in MyCallables class

public class MyCallables implements Callable<List<String>>{

    List<String> strList=new ArrayList<String>();

    @Override
    public List<String> call() throws Exception {   
        for(int i=0;i<=10;i++) {
            strList.add(Thread.currentThread().getName());      
        }
        return strList;
    }

}
Rowi
  • 545
  • 3
  • 9