Список возврата из Call () Callable - PullRequest
0 голосов
/ 02 июля 2019

Метод вызова Callable не возвращает список.Также, когда точка отладчика находится в строке return strList;в call (), он не приходит туда в любой момент времени.

Может ли кто-нибудь помочь, если он ошибается.

Заранее спасибо!

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

1 Ответ

3 голосов
/ 02 июля 2019

Вам нужно создать свой список в классе MyCallables

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

}
...