イドリス:
私はねじ山を介して他のクラスのメソッドを使用することにより、(他のクラスで行われた計算)素数を計算することができますマルチスレッドアプリケーションを構築しようとしています、私はその後、結果を印刷するために、他のクラスに結果を渡す必要があります。
私の問題は、私は(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);
}
期待される結果:
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
デッドプール :
はいこの行は無効ですfutures.get()
基本的にそれは、List<Future<List<Long>>> futures
リストFuture
オブジェクト。
あなたが取得する必要がありまずのでFuture
、リストからオブジェクトを、その後、あなたが値を取得する必要があるList<Long>
からFuture
オブジェクト
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());
またはループリストまたはイテレータリスト
for(Future<List<Long>> f : futures){
results.addAll(f.get());
}