java.lang.Object
cn.wjybxx.sequential.UniCancelTokenSource
- 所有已实现的接口:
ICancelToken,ICancelTokenSource
单线程版的取消令牌。
实现说明
1. 去除了code等的volatile操作,变更为普通字段。
2. 默认时间单位为毫秒。
3. 增加了重置状态的方法 -- 这对行为树这类应用非常有效。
4. 去除了递归通知的优化 -- 单线程下我们需要支持用户通过监听器引用取消注册;另外,我们假设子token的情况很少。
5. tryInline(cn.wjybxx.sequential.UniCancelTokenSource.Completion, java.util.concurrent.Executor, int)对executor的检测调整- 作者:
- wjybxx date - 2024/1/8
-
字段概要
从接口继承的字段 cn.wjybxx.concurrent.ICancelToken
NONE -
构造器概要
构造器构造器说明UniCancelTokenSource(int code) UniCancelTokenSource(UniScheduledExecutor executor) UniCancelTokenSource(UniScheduledExecutor executor, int code) -
方法概要
修饰符和类型方法说明返回一个只读的ICancelToken试图,返回的实例会在当前Token被取消时取消。boolean当前token是否可以进入取消状态intcancel()使用默认原因取消intcancel(int cancelCode) 将Token置为取消状态voidcancelAfter(int cancelCode, long millisecondsDelay) 在一段时间后发送取消命令voidcancelAfter(int cancelCode, long delay, TimeUnit timeUnit) 在一段时间后发送取消命令voidcancelAfter(int cancelCode, long delay, TimeUnit timeUnit, UniScheduledExecutor executor) int取消码 1.void检测取消信号 如果收到取消信号,则抛出CancellationExceptionintdegree()取消的紧急程度boolean是否已收到取消信号 任务的执行者将持有该令牌,在调度任务前会检测取消信号;如果任务已经开始,则由用户的任务自身检测取消和中断信号。boolean取消指令中是否要求了中断线程boolean取消指令中是否要求了无需删除newChild()创建一个子token,子token会在当前token被取消时取消。newInstance(boolean copyCode) 创建一个同类型实例。intreason()取消的原因 (1~10为底层使用,10以上为用户自定义)Tvoidreset()重置状态,以供复用setExecutor(UniScheduledExecutor executor) thenAccept(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) booleanunregister(Object action) 删除监听器 通常而言逆向查找更容易匹配:Task的停止顺序通常和Task的启动顺序相反,因此后注册的监听器会先删除。booleanunregister(Object action, boolean firstOccurrence) 删除监听器从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait从接口继承的方法 cn.wjybxx.concurrent.ICancelTokenSource
cancel, newInstance
-
构造器详细资料
-
UniCancelTokenSource
public UniCancelTokenSource() -
UniCancelTokenSource
public UniCancelTokenSource(int code) -
UniCancelTokenSource
-
UniCancelTokenSource
-
-
方法详细资料
-
getExecutor
-
setExecutor
-
unregister
删除监听器 通常而言逆向查找更容易匹配:Task的停止顺序通常和Task的启动顺序相反,因此后注册的监听器会先删除。 因此默认逆向查找匹配的监听器。- 参数:
action- 用户回调行为的引用
-
unregister
删除监听器- 参数:
action- 用户回调行为的引用firstOccurrence- 是否正向删除
-
reset
public void reset()重置状态,以供复用 -
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- 是否拷贝当前取消码- 返回:
- 取消令牌
-
newChild
创建一个子token,子token会在当前token被取消时取消。 1.该接口是构建实例和thenTransferTo(ICancelTokenSource)的快捷方法。 2.该接口用于快速构建子上下文。 -
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
-
cancelAfter
public void cancelAfter(int cancelCode, long millisecondsDelay) 在一段时间后发送取消命令- 指定者:
cancelAfter在接口中ICancelTokenSource- 参数:
cancelCode- 取消码millisecondsDelay- 延迟时间(毫秒) -- 单线程版的话,真实单位取决于约定。- 抛出:
IllegalStateException- 如果未绑定executor
-
cancelAfter
从接口复制的说明:ICancelTokenSource在一段时间后发送取消命令- 指定者:
cancelAfter在接口中ICancelTokenSource- 参数:
cancelCode- 取消码delay- 延迟时间timeUnit- 时间单位
-
cancelAfter
public void cancelAfter(int cancelCode, long delay, TimeUnit timeUnit, UniScheduledExecutor executor) - 参数:
executor- 用于延迟调度的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
-