Can't use the get mothod on my Future parameter to get result of thread with the callable interface

Idris :

I am trying to build a multithreading app that can calculate prime numbers (calculations done in another class), by using the methods of the other class via threadings, I need to then pass the result to the other class in order to print the results.

My issue is, my callable thread should be returning a list type, so when I try to use futures.get(), the compiler doesn't recognize my type of data

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

expected result:
results.add(futures.get()); should be working

But instead, I can't use futures.get()

Upon compilation, I get the following error:

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

Yes this line is invalid futures.get(), basically it is an List<Future<List<Long>>> futures list of Future object.

So First you need to get the Futureobject from list, and then you need to get the value List<Long> from Future object

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

Or loop the list or iterator the list

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=81870&siteId=1