gpt4 book ai didi

java - 完成 future : Unhandled exception type ExecutionException

转载 作者:行者123 更新时间:2023-12-01 17:57:44 30 4
gpt4 key购买 nike

我正在尝试探索 Java8 中的 CompletableFuture,我编写了这个简单的示例来使用假 api,但我收到了此编译错误,我在该代码周围添加了 try/catch block ,但我仍然得到相同的编译错误。

* 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 block ,编译错误不再显示,但是当我运行代码时它没有执行任何操作,看起来它正在等待某些东西......

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() 方法添加了一个 Timeout 参数,它开始抛出 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

最佳答案

您没有在正确的位置捕获异常。 map() 需要 Function 。并且函数不能抛出任何已检查的异常。所以你需要

.map(api-> {
try {
return api.get().getRandomText());
}
catch (ExecutionException e) {
// TODO: return something else or throw a runtime exception
}
})

关于java - 完成 future : Unhandled exception type ExecutionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43309358/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com