Java8でCompletableFutureを調べようとしていますが、偽のAPIを消費するこの簡単な例を書きましたが、このコンパイルエラーが発生しました。それでも同じコンパイルエラーが発生しています。CompletableFuture:未処理の例外タイプExecutionException
* FakeAPI1 *
package com.fake.api;
public class FakeAPI1 implements FakeAPI{
@Override
public void consume(){
try{
Thread.sleep(1000L);
System.out.println("Hello from FakeAPI1");
}catch(InterruptedException e){
System.out.println("Eat it silently");
}
}
public String getRandomText(){
System.out.println("getRandomText() @ FakeAPI1 was called ");
try{
Thread.sleep(1000L);
return "Hello from FakeAPI1";
}catch(InterruptedException e){
System.out.println("Eat it silently");
}
return "Default message from FakeAPI1";
}
}
* CompletableFutureTest *
package com.example.completablefuture;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import com.fake.api.FakeAPI1;
public class CompletableFutureTest {
public static void main(String[] args)
throws InterruptedException,ExecutionException {
List<Future<FakeAPI1>> apis=
Arrays.asList(
new CompletableFuture<FakeAPI1>(),
new CompletableFuture<FakeAPI1>(),
new CompletableFuture<FakeAPI1>()
);
Long start= System.currentTimeMillis();
apis.stream()
//Compilation error is in the line below
//Unhandled exception type ExecutionException
.map(api->api.get().getRandomText())
.collect(Collectors.toList());
Long end= System.currentTimeMillis();
System.out.println("CompletableFutureTest took " + (end-start) + " ms");
}
}
私はコンパイルエラーがもはや表示され、以下の回答で推奨されているようにtry/catchブロックを追加していませんしかし、コードを実行すると何もしません。何かを待っているようです...
package com.example.completablefuture;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import com.fake.api.FakeAPI1;
public class CompletableFutureTest {
public static void main(String[] args)
throws InterruptedException,ExecutionException {
List<CompletableFuture<FakeAPI1>> apis=
Arrays.asList(
new CompletableFuture<FakeAPI1>(),
new CompletableFuture<FakeAPI1>(),
new CompletableFuture<FakeAPI1>()
);
Long start= System.currentTimeMillis();
List<String> result= apis.stream()
.map(api-> {
try {
System.out.println("1");
api.get().getRandomText();
}catch (ExecutionException e) {
// TODO: return something else or throw a runtime exception
System.out.println("ExecutionException");
}catch(InterruptedException e){
// TODO: return something else or throw a runtime exception
System.out.println("InterruptedException");
}
return "NA";
})
.collect(Collectors.toList());
result.stream()
.forEach(System.out::println);
Long end= System.currentTimeMillis();
System.out.println("CompletableFutureTest took " + (end-start) + " ms");
}
}
私は、get()メソッドにタイムアウトパラメータを追加し、それはあなたが正しい場所に例外をキャッチしていない例外:InterruptedException
package com.example.completablefuture;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import com.fake.api.FakeAPI1;
public class CompletableFutureTest {
public static void main(String[] args)
throws InterruptedException,ExecutionException {
List<CompletableFuture<FakeAPI1>> apis=
Arrays.asList(
new CompletableFuture<FakeAPI1>(),
new CompletableFuture<FakeAPI1>(),
new CompletableFuture<FakeAPI1>()
);
Long start= System.currentTimeMillis();
List<String> result= apis.stream()
.map(api-> {
try {
System.out.println("about to call get() method ...");
api.get(1000L, TimeUnit.MILLISECONDS).getRandomText();
}catch (ExecutionException e) {
// TODO: return something else or throw a runtime exception
System.out.println("ExecutionException");
}catch(InterruptedException e){
// TODO: return something else or throw a runtime exception
System.out.println("InterruptedException");
}catch(TimeoutException e){
// TODO: return something else or throw a runtime exception
System.out.println("InterruptedException");
}
return "NA";
})
.collect(Collectors.toList());
result.stream()
.forEach(System.out::println);
Long end= System.currentTimeMillis();
System.out.println("CompletableFutureTest took " + (end-start) + " ms");
}
}
about to call get() method ...
InterruptedException
about to call get() method ...
InterruptedException
about to call get() method ...
InterruptedException
NA
NA
NA
CompletableFutureTest took 3062 ms
未処理の例外タイプExecutionException –
例外ではなく、コンパイルエラー –