hystrix源码小贴士之中断
2024-09-20 09:11:43
execution.isolation.thread.interruptOnCancel可以设置当cancellation发生时是否需要中断。通过Future的cancel方法和线程的中断方法来实现是否需要中断。
public Future<R> queue() {
/*
* The Future returned by Observable.toBlocking().toFuture() does not implement the
* interruption of the execution thread when the "mayInterrupt" flag of Future.cancel(boolean) is set to true;
* thus, to comply with the contract of Future, we must wrap around it.
*/
final Future<R> delegate = toObservable().toBlocking().toFuture(); final Future<R> f = new Future<R>() { @Override
public boolean cancel(boolean mayInterruptIfRunning) {
if (delegate.isCancelled()) {
return false;
} if (HystrixCommand.this.getProperties().executionIsolationThreadInterruptOnFutureCancel().get()) {
/*
* The only valid transition here is false -> true. If there are two futures, say f1 and f2, created by this command
* (which is super-weird, but has never been prohibited), and calls to f1.cancel(true) and to f2.cancel(false) are
* issued by different threads, it's unclear about what value would be used by the time mayInterruptOnCancel is checked.
* The most consistent way to deal with this scenario is to say that if *any* cancellation is invoked with interruption,
* than that interruption request cannot be taken back.
*/
interruptOnFutureCancel.compareAndSet(false, mayInterruptIfRunning);
} final boolean res = delegate.cancel(interruptOnFutureCancel.get()); if (!isExecutionComplete() && interruptOnFutureCancel.get()) {
final Thread t = executionThread.get();
if (t != null && !t.equals(Thread.currentThread())) {
t.interrupt();
}
} return res;
} @Override
public boolean isCancelled() {
return delegate.isCancelled();
} @Override
public boolean isDone() {
return delegate.isDone();
} @Override
public R get() throws InterruptedException, ExecutionException {
return delegate.get();
} @Override
public R get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return delegate.get(timeout, unit);
} };
execution.isolation.thread.interruptOnTimeout可以设置当发生timeout时,是否需要中断。通过getScheduler实现。
threadPool.getScheduler(new Func0<Boolean>() {
@Override
public Boolean call() {
return properties.executionIsolationThreadInterruptOnTimeout().get() && _cmd.isCommandTimedOut.get() == TimedOutStatus.TIMED_OUT;
}
})
private static class FutureCompleterWithConfigurableInterrupt implements Subscription {
private final FutureTask<?> f;
private final Func0<Boolean> shouldInterruptThread;
private final ThreadPoolExecutor executor; private FutureCompleterWithConfigurableInterrupt(FutureTask<?> f, Func0<Boolean> shouldInterruptThread, ThreadPoolExecutor executor) {
this.f = f;
this.shouldInterruptThread = shouldInterruptThread;
this.executor = executor;
} @Override
public void unsubscribe() {
executor.remove(f);
if (shouldInterruptThread.call()) {
f.cancel(true);
} else {
f.cancel(false);
}
} @Override
public boolean isUnsubscribed() {
return f.isCancelled();
}
}
最新文章
- Android开发中内存和UI优化
- vs配置
- C语言typeof详解 offsetof
- 在线API文档
- 安装JDK后JRE与JVM联系浅谈
- Colors
- 数据库中DDL、DML、DCL和TCP概念
- IOS 多线程,线程同步的三种方式
- Visual studio 内存不足的解决方案(out of memory)
- 如何让DbGrid支持鼠标滚轮滚动
- [SOJ] 商人的宣传
- 多线程进阶---Thread.join()/CountDownLatch.await() /CyclicBarrier.await()
- 将一个实体转换成 Url 参数的形式 ?a=a&;b=b
- ES6 for-of循环和迭代器使用细节
- CSS3 移动端 1PX 线变成0.5PX
- .NET-记一次架构优化实战与方案-前端优化
- Hibernate 再接触 事务隔离机制
- v$instance如何生成
- HDU 2569(简单的递推)
- [na]tcpdump非常实用的抓包实例
热门文章
- Windows server 2008R2 中sql server的搭建
- linux,运维,部署 相关
- springMVC入门(六)------json交互与RESTFul风格支持
- First-Spike-Based Visual Categorization Using Reward-Modulated STDP
- python连接mysql中报错1064修改方法
- Qt 最简单的多线程方法QtConcurrent::run()
- 【深入理解Java虚拟机】Java虚拟机运行时数据区
- Codeforces1409 题解(A-F)
- Oracle数据库时提示“ORA-28000
- 一招教你如何用Word直接打开PDF进行编辑,无需下载转换软件