Java Can';t在我的Future参数上使用get方法来获取带有可调用接口的线程的结果

Java Can';t在我的Future参数上使用get方法来获取带有可调用接口的线程的结果,java,multithreading,future,callable,Java,Multithreading,Future,Callable,我正在尝试构建一个多线程应用程序,它可以计算素数(在另一个类中完成的计算),通过使用另一个类的方法通过线程,然后我需要将结果传递给另一个类以打印结果 我的问题是,我的可调用线程应该返回列表类型,因此当我尝试使用futures.get()时,编译器无法识别我的数据类型 ExecutorService executor = Executors.newFixedThreadPool(10); Callable<List<Long>> callableTask = () -&g

我正在尝试构建一个多线程应用程序,它可以计算素数(在另一个类中完成的计算),通过使用另一个类的方法通过线程,然后我需要将结果传递给另一个类以打印结果

我的问题是,我的可调用线程应该返回列表类型,因此当我尝试使用futures.get()时,编译器无法识别我的数据类型

ExecutorService executor = Executors.newFixedThreadPool(10);

Callable<List<Long>> callableTask = () -> {

            List<Long> myLis = new ArrayList<>();
            try
            {

                PrimeComputerTester pct = new PrimeComputerTester() 
                Method meth = PrimeComputerTester.class.getDeclaredMethod("getPrimes",long.class);
                meth.setAccessible(true);

                myLis = (List<Long>) meth.invoke(pct, max);


                //System.out.println("List of prime numbers: ");
                //for(int i = 0; i < myLis.size(); i++)
                 //  System.out.println(myLis.get(i));

            }catch (Exception e) 
            {
                e.printStackTrace();
                System.out.println(" interrupted");
            }

    return myLis;  //the thread should be returning myList
};


//using the list<Long> type for my callable interface

List<Callable<List<Long>>> callableTasks = new ArrayList<>();

//creating a tasked thread
callableTasks.add(callableTask);



  try {
      List<Future<List<Long>>> futures = executor.invokeAll(callableTasks);

      List<Long> results = new ArrayList<>();

      results.add(futures.get());   //This line doesn't work

      //System.out.println("List of prime numbers 2 : "+futures.get());
       for(int i = 0; i < futures.size(); i++)
                   System.out.println(futures.get(i));
     executor.shutdown();
     //   System.out.println(" interrupted");


  } catch (InterruptedException ex) {
      Logger.getLogger(PrimeComputer.class.getName()).log(Level.SEVERE, null, ex);
  }
ExecutorService executor=Executors.newFixedThreadPool(10);
可调用可调用任务=()->{
List myLis=new ArrayList();
尝试
{
PrimeComputerTester pct=新PrimeComputerTester()
方法meth=PrimeComputerTester.class.getDeclaredMethod(“getPrimes”,long.class);
方法设置可访问(真);
myLis=(列表)方法调用(pct,max);
//System.out.println(“素数列表:”);
//对于(int i=0;i
预期结果:
results.add(futures.get()); 应该可以工作了

但是相反,我不能使用futures.get()

编译后,我得到以下错误:

 method get int interface Liste <E> cannot be applied to given types;

 required int

 found: no arguments

 reason: actual and formal argument lists differ in length
 where E is a type-variable:
 E extends Object declared in interface List
方法get int interface Liste不能应用于给定类型;
必需整数
找到:没有参数
原因:实际参数列表和正式参数列表长度不同
其中E是一个类型变量:
扩展接口列表中声明的对象

是这行是无效的
futures.get()
,基本上它是
List futures
对象的
列表

因此,首先需要从列表中获取
Future
对象,然后需要从
Future
对象中获取值
list

Future<List<Long>> f = futures.get(0);   // get the object at index 1 if list is empty then you will get NullPointerExeception
results.addAll(f.get());
Future f=futures.get(0);//获取索引1处的对象如果列表为空,则将获取NullPointerExeception
results.addAll(f.get());
或循环列表或迭代列表

for(Future<List<Long>> f : futures){
      results.addAll(f.get());
   }
for(期货f:期货){
results.addAll(f.get());
}

你说的“这一行不工作”是什么意思?不编译或引发异常?诊断是什么?@AlexeiKaigorodov我刚刚编辑了输出谢谢你这么多,至少花了3个小时尝试调试,顺便说一句,正确答案应该是:Future f=futures.get(0);由于0是列表的第一个元素,1抛出一个越界异常。为了更容易地遍历它,我还使用了results=f.get(),而不是您提出的;