java.lang.Object
cn.wjybxx.concurrent.CancelTokenSource
- 所有已实现的接口:
ICancelToken,ICancelTokenSource
取消令牌源由任务的创建者(发起者)持有,具备取消权限。
实现说明
这里的实现是Promise的翻版,但不同的是:取消令牌需要支持删除监听,而且取消令牌存在频繁增删监听的情况!
由于实现高效且安全的删除并不容易,这里暂时采用延迟删除的方案。- 作者:
- wjybxx date - 2024/1/8
-
字段概要
从接口继承的字段 cn.wjybxx.concurrent.ICancelToken
NONE -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明返回一个只读的ICancelToken试图,返回的实例会在当前Token被取消时取消。boolean当前token是否可以进入取消状态intcancel()使用默认原因取消intcancel(boolean mayInterruptIfRunning) 该方法主要用于兼容JDKintcancel(int cancelCode) 将Token置为取消状态voidcancelAfter(int cancelCode, long millisecondsDelay) 在一段时间后发送取消命令voidcancelAfter(int cancelCode, long delay, TimeUnit timeUnit) 在一段时间后发送取消命令 (将由默认的调度器调度)voidcancelAfter(int cancelCode, long delay, TimeUnit timeUnit, ScheduledExecutorService executor) int取消码 1.void检测取消信号 如果收到取消信号,则抛出CancellationExceptionintdegree()取消的紧急程度boolean是否已收到取消信号 任务的执行者将持有该令牌,在调度任务前会检测取消信号;如果任务已经开始,则由用户的任务自身检测取消和中断信号。boolean取消指令中是否要求了中断线程boolean取消指令中是否要求了无需删除newInstance(boolean copyCode) 创建一个同类型实例。intreason()取消的原因 (1~10为底层使用,10以上为用户自定义)TthenAccept(BiConsumer<? super ICancelToken, Object> action, Object ctx) thenAccept(BiConsumer<? super ICancelToken, Object> action, Object ctx, int options) thenAccept(Consumer<? super ICancelToken> action) thenAccept(Consumer<? super ICancelToken> action, int options) 添加的action将在Context收到取消信号时执行 1.如果已收到取消请求,则给定的action会立即执行。thenAcceptAsync(Executor executor, BiConsumer<? super ICancelToken, Object> action, Object ctx) thenAcceptAsync(Executor executor, BiConsumer<? super ICancelToken, Object> action, Object ctx, int options) thenAcceptAsync(Executor executor, Consumer<? super ICancelToken> action) thenAcceptAsync(Executor executor, Consumer<? super ICancelToken> action, int options) thenNotify(CancelTokenListener action) thenNotify(CancelTokenListener action, int options) 添加一个特定类型的监听器 (用于特殊需求时避免额外的闭包 - task经常需要监听取消令牌)thenNotifyAsync(Executor executor, CancelTokenListener action) thenNotifyAsync(Executor executor, CancelTokenListener action, int options) thenRunAsync(Executor executor, Runnable action) thenRunAsync(Executor executor, Runnable action, int options) thenRunAsync(Executor executor, Consumer<Object> action, Object ctx) thenRunAsync(Executor executor, Consumer<Object> action, Object ctx, int options) thenTransferTo(ICancelTokenSource child) thenTransferTo(ICancelTokenSource child, int options) 该接口用于方便构建子上下文 1.子token会在当前token进入取消状态时被取消 2.该接口本质是一个快捷方法,但允许子类优化thenTransferToAsync(Executor executor, ICancelTokenSource child) thenTransferToAsync(Executor executor, ICancelTokenSource child, int options) 从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait从接口继承的方法 cn.wjybxx.concurrent.ICancelTokenSource
newInstance
-
构造器详细资料
-
CancelTokenSource
public CancelTokenSource() -
CancelTokenSource
public CancelTokenSource(int code)
-
-
方法详细资料
-
asReadonly
从接口复制的说明:ICancelToken返回一个只读的ICancelToken试图,返回的实例会在当前Token被取消时取消。 其作用类似IFuture.asReadonly()- 指定者:
asReadonly在接口中ICancelToken
-
canBeCancelled
public boolean canBeCancelled()从接口复制的说明:ICancelToken当前token是否可以进入取消状态- 指定者:
canBeCancelled在接口中ICancelToken- 返回:
- 如果当前token可以进入取消状态则返回true
-
newInstance
从接口复制的说明:ICancelTokenSource创建一个同类型实例。 1.原型对象,避免具体类型依赖。 2.默认情况下,其它上下文应当拷贝。- 指定者:
newInstance在接口中ICancelTokenSource- 参数:
copyCode- 是否拷贝当前取消码- 返回:
- 取消令牌
-
cancel
public int cancel(int cancelCode) 将Token置为取消状态- 指定者:
cancel在接口中ICancelTokenSource- 参数:
cancelCode- 取消码;reason部分需大于0;辅助类CancelCodeBuilder- 返回:
- 如果Token已被取消,则返回旧值(大于0);如果Token尚未被取消,则将Token更新为取消状态,并返回0。
- 抛出:
IllegalArgumentException- 如果code小于等于0;或reason部分为0UnsupportedOperationException- 如果context是只读的
-
cancel
public int cancel()使用默认原因取消- 指定者:
cancel在接口中ICancelTokenSource
-
cancel
public int cancel(boolean mayInterruptIfRunning) 该方法主要用于兼容JDK- 指定者:
cancel在接口中ICancelTokenSource- 参数:
mayInterruptIfRunning- 是否可以中断目标线程;注意该参数由任务自身处理,且任务监听了取消信号才有用
-
cancelAfter
public void cancelAfter(int cancelCode, long millisecondsDelay) 从接口复制的说明:ICancelTokenSource在一段时间后发送取消命令- 指定者:
cancelAfter在接口中ICancelTokenSource- 参数:
cancelCode- 取消码millisecondsDelay- 延迟时间(毫秒) -- 单线程版的话,真实单位取决于约定。
-
cancelAfter
在一段时间后发送取消命令 (将由默认的调度器调度)- 指定者:
cancelAfter在接口中ICancelTokenSource- 参数:
cancelCode- 取消码delay- 延迟时间timeUnit- 时间单位
-
cancelAfter
public void cancelAfter(int cancelCode, long delay, TimeUnit timeUnit, ScheduledExecutorService executor) -
cancelCode
public int cancelCode()从接口复制的说明:ICancelToken取消码 1. 按bit位存储信息,包括是否请求中断,是否超时,紧急程度等 2. 低20位为取消原因;高12位为特殊信息CancelCodes.MASK_REASON3. 不为0表示已发起取消请求 4. 取消时至少赋值一个信息,reason通常应该赋值- 指定者:
cancelCode在接口中ICancelToken
-
isCancelling
public boolean isCancelling()从接口复制的说明:ICancelToken是否已收到取消信号 任务的执行者将持有该令牌,在调度任务前会检测取消信号;如果任务已经开始,则由用户的任务自身检测取消和中断信号。- 指定者:
isCancelling在接口中ICancelToken
-
reason
public int reason()从接口复制的说明:ICancelToken取消的原因 (1~10为底层使用,10以上为用户自定义)T- 指定者:
reason在接口中ICancelToken
-
degree
public int degree()从接口复制的说明:ICancelToken取消的紧急程度- 指定者:
degree在接口中ICancelToken
-
isInterruptible
public boolean isInterruptible()从接口复制的说明:ICancelToken取消指令中是否要求了中断线程- 指定者:
isInterruptible在接口中ICancelToken
-
isWithoutRemove
public boolean isWithoutRemove()从接口复制的说明:ICancelToken取消指令中是否要求了无需删除- 指定者:
isWithoutRemove在接口中ICancelToken
-
checkCancel
public void checkCancel()从接口复制的说明:ICancelToken检测取消信号 如果收到取消信号,则抛出CancellationException- 指定者:
checkCancel在接口中ICancelToken
-
thenAccept
从接口复制的说明:ICancelToken添加的action将在Context收到取消信号时执行 1.如果已收到取消请求,则给定的action会立即执行。 2.如果尚未收到取消请求,则给定action会在收到请求时执行。- 指定者:
thenAccept在接口中ICancelToken
-
thenAccept
- 指定者:
thenAccept在接口中ICancelToken
-
thenAcceptAsync
- 指定者:
thenAcceptAsync在接口中ICancelToken
-
thenAcceptAsync
public IRegistration thenAcceptAsync(Executor executor, Consumer<? super ICancelToken> action, int options) - 指定者:
thenAcceptAsync在接口中ICancelToken
-
thenAccept
public IRegistration thenAccept(BiConsumer<? super ICancelToken, Object> action, Object ctx, int options) - 指定者:
thenAccept在接口中ICancelToken
-
thenAccept
- 指定者:
thenAccept在接口中ICancelToken
-
thenAcceptAsync
public IRegistration thenAcceptAsync(Executor executor, BiConsumer<? super ICancelToken, Object> action, Object ctx) - 指定者:
thenAcceptAsync在接口中ICancelToken
-
thenAcceptAsync
public IRegistration thenAcceptAsync(Executor executor, BiConsumer<? super ICancelToken, Object> action, Object ctx, int options) - 指定者:
thenAcceptAsync在接口中ICancelToken
-
thenRun
- 指定者:
thenRun在接口中ICancelToken
-
thenRun
- 指定者:
thenRun在接口中ICancelToken
-
thenRunAsync
- 指定者:
thenRunAsync在接口中ICancelToken
-
thenRunAsync
- 指定者:
thenRunAsync在接口中ICancelToken
-
thenRun
- 指定者:
thenRun在接口中ICancelToken
-
thenRun
- 指定者:
thenRun在接口中ICancelToken
-
thenRunAsync
- 指定者:
thenRunAsync在接口中ICancelToken
-
thenRunAsync
public IRegistration thenRunAsync(Executor executor, Consumer<Object> action, Object ctx, int options) - 指定者:
thenRunAsync在接口中ICancelToken
-
thenNotify
从接口复制的说明:ICancelToken添加一个特定类型的监听器 (用于特殊需求时避免额外的闭包 - task经常需要监听取消令牌)- 指定者:
thenNotify在接口中ICancelToken
-
thenNotify
- 指定者:
thenNotify在接口中ICancelToken
-
thenNotifyAsync
- 指定者:
thenNotifyAsync在接口中ICancelToken
-
thenNotifyAsync
- 指定者:
thenNotifyAsync在接口中ICancelToken
-
thenTransferTo
- 指定者:
thenTransferTo在接口中ICancelToken
-
thenTransferTo
从接口复制的说明:ICancelToken该接口用于方便构建子上下文 1.子token会在当前token进入取消状态时被取消 2.该接口本质是一个快捷方法,但允许子类优化注意:在Future体系下,child是上游任务;而在行为树这类体系下,child是下游任务。
- 指定者:
thenTransferTo在接口中ICancelToken- 参数:
child- 接收结果的子tokenoptions- 调度选项
-
thenTransferToAsync
- 指定者:
thenTransferToAsync在接口中ICancelToken
-
thenTransferToAsync
- 指定者:
thenTransferToAsync在接口中ICancelToken
-