线程如何在Spring异步函数中成功终止,以及如何在java的多线程方法中递归调用同一函数

线程如何在Spring异步函数中成功终止,以及如何在java的多线程方法中递归调用同一函数,java,multithreading,spring-boot,asynchronous,completable-future,Java,Multithreading,Spring Boot,Asynchronous,Completable Future,我正在从控制器调用@Async函数,并且在完成HttpServiceClient类中的makecallwithrestemplate方法后,线程似乎没有终止 问题1-如何确保线程在执行任务后关闭 我有两种类型的任务-独立任务和依赖任务。 现在我的要求是,我将获取任务列表作为输入,我需要首先执行所有独立任务,然后根据如下条件执行依赖任务 问题2-一旦所有独立任务都执行完毕,在这里等待,现在我的问题是如何在这里等待 问题3-一旦等待完成,再次调用restTemplate.exchange方法和相关

我正在从控制器调用
@Async
函数,并且在完成
HttpServiceClient
类中的
makecallwithrestemplate
方法后,线程似乎没有终止

  • 问题1-如何确保线程在执行任务后关闭
  • 我有两种类型的任务-独立任务和依赖任务。 现在我的要求是,我将获取任务列表作为输入,我需要首先执行所有独立任务,然后根据如下条件执行依赖任务
  • 问题2-一旦所有独立任务都执行完毕,在这里等待,现在我的问题是如何在这里等待

  • 问题3-一旦等待完成,再次调用restTemplate.exchange方法和相关任务,如何使其递归,我的hashmap在递归中是否是线程安全的

  • 问题4-
    makeCallWithRestTemplate
    方法中,我正在使用hashmap,它将进一步传递到提取的私有方法,现在我的下一个问题是,hashmap在多线程环境中是否安全,因为对我来说,它是一个局部变量,对于不同的线程会有所不同?或者我需要在这里使用ConcurrentHashmap吗?如何测试它
请求结构

[ 
    { independent task 1
    },
    { dependent task 1,
        "dependency": { // dependency is that, execute task 1 first 
                        and then based on its result code execute dependent task 1
            "===": [
            "$[0].response.http_body.action.result_code",
            "SUCCESS"
            ]
        }
    }
]
控制器

@RestController
@RequestMapping("/abc")
public class ActionController {
    @Autowired
    private HttpServiceClient client;
    @PostMapping
    public ResponseEntity<JsonNode> performMultipleActions(@RequestBody List<List<ABC>> abc) {
            return client.callExternalService(abc);
    }
}

@Component
public class HttpServiceClient {
    @Autowired
    private RestTemplate restTemplate; 
    @Async
    public CompletableFuture<Map<Integer, ResponseEntity<JsonNode>>> makeCallWithRestTemplate(
            List<ABC> requests) throws IOException { 
        int index = -1;
        Map map = new hashmap();
    while (iterator.hasNext()) { 
        index++;
        Abc abc = iterator.next();
        if (dependency == null) {
           extracted(mapOfResult, index, action);
        }else{
        //create a map of with index as a key and object as a value
        map.put(index, request); 
        }
        // **Question 3-** once all the independent task will get executed wait here, Now my question is how to wait here ? 
        //**Question 4-** once wait complete again call restTemplate.exchange method with dependent task, how can i make it recursive, will my hashmap would be threadsafe in recursive?
    }
    return CompletableFuture.completedFuture(exchange);
    }
}
private void extracted(Map<Integer, ResponseEntity<JsonNode>> mapOfResult, int index, ActionRequest action)
            throws JsonProcessingException, JsonMappingException {
 ResponseEntity<JsonNode> exchange = restTemplate.exchange(urlObj,
                            HttpMethod.resolve(action.getMethod()), entity, JsonNode.class); 
    mapOfResult.put(index, exchange);
}
@RestController
@请求映射(“/abc”)
公共类ActionController{
@自动连线
私有HttpServiceClient;
@邮戳
公共响应执行多项操作(@RequestBody List abc){
返回client.callExternalService(abc);
}
}
@组成部分
公共类HttpServiceClient{
@自动连线
私有RestTemplate RestTemplate;
@异步的
公共CompletableFuture makeCallWithRestTemplate(
列表请求)引发IOException{
int指数=-1;
Map Map=newhashmap();
while(iterator.hasNext()){
索引++;
Abc=iterator.next();
if(依赖项==null){
提取(映射结果、索引、操作);
}否则{
//创建的映射,索引作为键,对象作为值
map.put(索引、请求);
}
//**问题3-**一旦所有独立任务都执行完毕,请在此处等待,现在我的问题是如何在此处等待?
//**问题4-**一旦等待完成,再次调用restTemplate.exchange方法和相关任务,如何使其递归,我的hashmap在递归中是否是线程安全的?
}
返回CompletableFuture.completedFuture(交换);
}
}
已提取私有void(映射mapOfResult、int索引、ActionRequest操作)
抛出JsonProcessingException、JsonMappingException{
ResponseEntity exchange=restemplate.exchange(urlObj,
解析(action.getMethod()),实体,JsonNode.class);
结果权(索引、交换)的映射;
}

1。你不需要,框架会处理的,2。使用一个完整的未来并加入他们。总的来说,您正在构建一个太复杂、太灵活而无法处理的任务。@Deinum感谢您的快速响应,我有没有办法解决问题3,它可以在N个独立和依赖任务中工作?