Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 是春天';s ThreadPoolTaskExecutor非阻塞?_Spring_Spring Mvc_Asynchronous - Fatal编程技术网

Spring 是春天';s ThreadPoolTaskExecutor非阻塞?

Spring 是春天';s ThreadPoolTaskExecutor非阻塞?,spring,spring-mvc,asynchronous,Spring,Spring Mvc,Asynchronous,如果我将回调方法添加到从用springs@Async注释的方法返回的ListenableFuture中,那么@Async使用的线程池中的线程是否会被阻塞,直到回调被返回,或者该线程是否会被返回到线程池中以便在回调被调用之前重用 我不是询问控制器使用的线程是否返回到servlet。我只询问ThreadPoolTaskExecutor中的线程: @Controller @EnableAutoConfiguration public class ListenableFutureAsyncControl

如果我将回调方法添加到从用springs@Async注释的方法返回的ListenableFuture中,那么@Async使用的线程池中的线程是否会被阻塞,直到回调被返回,或者该线程是否会被返回到线程池中以便在回调被调用之前重用

我不是询问控制器使用的线程是否返回到servlet。我只询问ThreadPoolTaskExecutor中的线程:

@Controller
@EnableAutoConfiguration
public class ListenableFutureAsyncController {

    @Autowired
    IHeavyLiftingService heavyLiftingService;

    @RequestMapping("/")
    @ResponseBody
    DeferredResult<String> home() {
        // Create DeferredResult
        final DeferredResult<String> result = new DeferredResult<>();

        //Call to the async service
        ListenableFuture<ResponseEntity<String>> future = heavyLiftingService.heavyLifting();

        future.addCallback(
          new ListenableFutureCallback<ResponseEntity<String>>() {
            @Override
            public void onSuccess(ResponseEntity<String> response) {
                result.setResult(response.getBody());
            }

            @Override
            public void onFailure(Throwable t) {
                result.setErrorResult(t.getMessage());
            }
        });
        // Return the thread to servlet container, 
        // the response will be processed by another thread.
        return result;
    }

}
@控制器
@启用自动配置
公共类ListenableFutureAyncController{
@自动连线
i重型提升服务重型提升服务;
@请求映射(“/”)
@应答器
延迟结果主页(){
//创建延迟结果
最终递延结果=新递延结果();
//调用异步服务
ListenableFuture=heavyLiftingService.heavyLifting();
future.addCallback(
新建ListenableFutureCallback(){
@凌驾
成功时的公共无效(响应性响应){
result.setResult(response.getBody());
}
@凌驾
失效时的公共无效(可丢弃的t){
result.setErrorResult(t.getMessage());
}
});
//将线程返回到servlet容器,
//响应将由另一个线程处理。
返回结果;
}
}
实现@Async方法的服务

@Service
public class HeavyLiftingServiceImpl implements IHeavyLiftingService {

    @Async(value="asyncServiceThreadPoolTaskExecutor")
    public ListenableFuture<String> heavyLifting() {
        ...
        ...
        ...
    }
}
@服务
公共类HeavyLiftingServiceImpl实现IHeavyLiftingService{
@异步(value=“asyncServiceThreadPoolTaskExecutor”)
公开上市未来重型运输(){
...
...
...
}
}
asyncServiceThreadPoolTaskExecutor是org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor的实例


如果可能,请提供描述此行为的文档链接。

您是否解决了此问题?我也有同样的疑问。是的,从asyncServiceThreadPoolTaskExecutor获取的线程将被阻塞,直到heavyLifting方法完成(工作必须在正确的地方完成?),一旦工作完成,线程将被释放回线程池供另一个任务使用。如果你告诉我你想做什么,我也许能给出更详细的答案。