监听一个方法 执行规定时间后没返回,强制超时返回错误信息
public String methodRuntimeTest() {
AtomicReference<String> str= new AtomicReference<>("00000");
CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(this::doAnyThing).whenComplete((a,b)->{
System.out.println("执行结束");
str.set(a);
});
try {
stringCompletableFuture.get(3, TimeUnit.SECONDS);
}catch (Exception e){
System.out.println("执行操时");
return str.get();
}
return str.get();
}
public String doAnyThing(){
try {
Thread.sleep(1000);
}catch (Exception e){
return "33333";
}
return "33333";
}