Java background time-consuming task multithreading returns results

  java, question
@RequestMapping(value = "ehr", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON)
 @ResponseBody
 public User getUser() {
 User u = new User();
 Future<User> future = poolTaskExecutor.submit(ehrDownloadTask);
 boolean flag = future.isDone();
 while (!  flag) {
 U.setMsg ("incomplete");
 return u;
 bracket
 try {
 u = future.get();
 } catch (Exception e) {
 e.printStackTrace();
 bracket
 Out.println  );
 return u;
 bracket

Suppose I request a time-consuming task to be executed in the thread pool. I think if it is not finished, the foreground can refresh the page to show that it is not finished. However, as soon as it is refreshed, I start a new thread to execute, so I have been returning to unfinished. Can you tell me how to write this?

Write a simple implementation:

private static Map<String,Future<User>> futures = new HashMap<>();
 
 @RequestMapping(value = "ehr", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON)
 @ResponseBody
 public User getUser(HttpServletRequest request, HttpServletResponse response) {
 String sessionID = request.getSession().getId();
 
 User u = new User();
 U.setMsg ("incomplete");
 if(futures.containsKey(sessionID)) {
 Future<User> userFuture = futures.get(sessionID);
 if(userFuture.isDone()) {
 try {
 u = userFuture.get();
 } catch (InterruptedException | ExecutionException e) {
 e.printStackTrace();
 bracket
 futures.remove(sessionID);
 bracket
 } else {
 Future<User> future = poolTaskExecutor.submit(ehrDownloadTask);
 futures.put(sessionID,future);
 bracket
 return u;
 bracket

Front desk rotation until correct return.
Key You can use a SessionID like mine, or you can generate it yourself according to logic.
If it is distributed Tomcat, Redis can be used instead of HashMap.