揭秘Future cancel迷惑性boolean入参~

前言

当我们使用线程池submit一个任务后,会返回一个Future,而在Future接口中存在一个cancel方法,来帮助我们取消掉任务。

但是cancel方法有一个boolean类型的入参,比较迷惑,之前也了解过该入参true 和 false的区别,但过一段时间之后就又忘了,遂写了本文进行记录,顺便了解下源码~

/**
 * Attempts to cancel execution of this task.  This attempt will
 * fail if the task has already completed, has already been cancelled,
 * or could not be cancelled for some other reason. If successful,
 * and this task has not started when {@code cancel} is called,
 * this task should never run.  If the task has already started,
 * then the {@code mayInterruptIfRunning} parameter determines
 * whether the thread executing this task should be interrupted in
 * an attempt to stop the task.
 *
 * <p>After this method returns, subsequent calls to {@link #isDone} will
 * always return {@code true}.  Subsequent calls to {@link #isCancelled}
 * will always return {@code true} if this method returned {@code true}.
 *
 * @param mayInterruptIfRunning {@code true} if the thread executing this
 * task should be interrupted; otherwise, in-progress tasks are allowed
 * to complete
 * @return {@code false} if the task could not be cancelled,
 * typically because it has already completed normally;
 * {@code true} otherwise
 */
boolean cancel(boolean mayInterruptIfRunning);
复制代码

上面是cancel方法的接口定义,当然英文看着麻烦,咱直接翻译成看得懂的~

  • cancel方法,会尝试取消任务的执行,但如果任务已经完成、已经取消或其他原因无法取消,则尝试取消任务失败。

  • 如果取消成功,并且在取消时

    • 该任务还未执行,那么这个任务永远不会执行。
    • 如果该任务已经启动,那么会根据cancelboolean入参来决定是否中断执行此任务的线程
在 Spring Boot 中,你可以使用拦截器(Interceptor)来修改 Controller 的入参。拦截器可以在请求到达 Controller 之前或之后进行处理。 首先,你需要创建一个实现了 `HandlerInterceptor` 接口的拦截器类。在拦截器中,你可以通过重写 `preHandle` 方法,在请求到达 Controller 之前进行处理,并修改 Controller 的入参。 以下是一个示例代码: ```java public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; MethodParameter[] methodParameters = handlerMethod.getMethodParameters(); for (MethodParameter parameter : methodParameters) { if (parameter.getParameterType().equals(String.class)) { String originalValue = request.getParameter(parameter.getParameterName()); String modifiedValue = modifyValue(originalValue); request.setAttribute(parameter.getParameterName(), modifiedValue); } } } return true; } private String modifyValue(String value) { // 在这里对入参进行修改 // 例如,可以替换特定的字符串、添加前缀或后缀等 return value; } } ``` 然后,在你的配置类中注册拦截器: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Autowired private MyInterceptor myInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor); } } ``` 通过以上配置,`MyInterceptor` 中的 `preHandle` 方法会在请求到达 Controller 之前进行拦截并修改入参。你可以在该方法中根据需要对入参进行相应的修改操作。 希望这对你有帮助!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值