Java 在移动到下一个语句之前,请等待数据库更新的结果

Java 在移动到下一个语句之前,请等待数据库更新的结果,java,servlets,Java,Servlets,我希望我的servlet在移动到下一行之前等待数据库更新的结果。 我有以下代码片段: //wait for this to finish and get the status status=profiledao.updateProfile(profile); //then execute this statement httpresponse.getWriter().print(fileName); 最好的方法是什么 谢谢。除非您使用其他未发布的代码异步运行这一行,否则您发布的代码将完全

我希望我的servlet在移动到下一行之前等待数据库更新的结果。 我有以下代码片段:

//wait for this to finish and get the status
status=profiledao.updateProfile(profile);

//then execute this statement
httpresponse.getWriter().print(fileName);  
最好的方法是什么


谢谢。

除非您使用其他未发布的代码异步运行这一行,否则您发布的代码将完全符合您在普通Java环境中的预期

如果您以异步方式运行此操作,并且希望在完成所述任务的同时继续这样做,那么您需要的是所谓的承诺

CompletableFuture.supplyAsync(this::sendMsg)  
             .thenAccept(this::notify);

此代码是在异步设置中执行此操作的一种非常简单的方法,但是,如果显示的代码是同步的,则不需要此代码。

默认行为是您想要的。Java调用是阻塞的,除非您专门将它们设置为非阻塞(例如,标记为@Asynchronous的EJB方法)。它不是在等待当前代码吗?