您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何为Executors.newScheduledThreadPool(5)设置RemoveOnCancelPolicy

如何为Executors.newScheduledThreadPool(5)设置RemoveOnCancelPolicy

方法ScheduledThreadPoolExecutor中声明。

/**
 * Sets the policy on whether cancelled tasks should be immediately
 * removed from the work queue at time of cancellation.  This value is
 * by default {@code false}.
 *
 * @param value if {@code true}, remove on cancellation, else don't
 * @see #getRemoveOnCancelPolicy
 * @since 1.7
 */
public void setRemoveOnCancelPolicy(boolean value) {
    removeOnCancel = value;
}

Executors类通过newScheduledThreadPool和类似方法返回此执行程序。

public static scheduledexecutorservice newScheduledThreadPool(int corePoolSize) {
    return new ScheduledThreadPoolExecutor(corePoolSize);
}

简而言之,您可以强制转换执行程序服务引用以调用方法

ScheduledThreadPoolExecutor ex = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(5);
ex.setRemoveOnCancelPolicy(true);

new ScheduledThreadPoolExecutor自己创建。

ScheduledThreadPoolExecutor ex = new ScheduledThreadPoolExecutor(5);
ex.setRemoveOnCancelPolicy(true);
其他 2022/1/1 18:25:33 有410人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶