hystrix源码小贴士之中断
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>() {@Overridepublic 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;}@Overridepublic boolean isCancelled() {return delegate.isCancelled();}@Overridepublic boolean isDone() {return delegate.isDone();}@Overridepublic R get() throws InterruptedException, ExecutionException {return delegate.get();}@Overridepublic 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>() {@Overridepublic 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;}@Overridepublic void unsubscribe() {executor.remove(f);if (shouldInterruptThread.call()) {f.cancel(true);} else {f.cancel(false);}}@Overridepublic boolean isUnsubscribed() {return f.isCancelled();}}
转载于:https://www.cnblogs.com/zhangwanhua/p/8116997.html
hystrix源码小贴士之中断相关推荐
- hystrix源码小贴士之Yammer Publisher
HystrixYammerMetricsPublisher 继承HystrixMetricsPublisher,创建HystrixYammerMetricsPublisherCommand.Hystr ...
- Java熔断框架有哪些_降级熔断框架 Hystrix 源码解析:滑动窗口统计
降级熔断框架 Hystrix 源码解析:滑动窗口统计 概述 Hystrix 是一个开源的降级熔断框架,用于提高服务可靠性,适用于依赖大量外部服务的业务系统.什么是降级熔断呢? 降级 业务降级,是指牺牲 ...
- hystrix 源码 线程池隔离_Spring Cloud Hystrix 源码学习合集
# Spring Cloud Hystrix 源码学习合集 **Hystrix: Latency and Fault Tolerance for Distributed Systems** ![](h ...
- hystrix 源码 线程池隔离_Hystrix源码学习--线程池隔离
分析你的系统 你所认识的分布式系统,哪些是可以进行垂直拆分的?拆分之后系统之间的依赖如何梳理?系统异构之后的稳定性调用如何保证?这些都是可能在分布式场景中面临的问题. 说个比较常见的问题,大家都知道秒 ...
- 熔断器 Hystrix 源码解析 —— 命令执行(三)之执行超时
2019独角兽企业重金招聘Python工程师标准>>> 摘要: 原创出处 http://www.iocoder.cn/Hystrix/command-execute-third-ti ...
- Java智慧校园系统源码,智慧学校源码 +小程序+电子班牌
Java智慧校园系统源码 智慧学校源码 小程序+电子班牌 有演示,可正常上线运营+可授权 开发环境:Java+springboot+vue+element-ui+mysql 一款集智慧教学.智慧教务. ...
- 自采集壁纸网站源码 - 小韩美化版
介绍: 自采集壁纸网站源码 - 小韩美化版 网盘下载地址: https://zijiewangpan.com/jvhkDTFdngV 图片: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下 ...
- Java开发多、单商户淘宝客+任务系统cps系统源码小程序app
Java开发的多商户淘宝客.任务cps系统源码淘宝客+美团外卖+饿了么外卖+多麦CPS+京东+拼多多+多商户自营+任务墙+本地生活模块+抖音+唯品会+文娱充值+话费充值小程序app主要包括用户端.商家 ...
- 三. 微服务源码阅读-Hystrix 源码
3. Hystrix 源码 1. 断路器开关 @SpringBootApplication(scanBasePackages = {"len.hgy"}) //注册到eureka ...
最新文章
- 【CV】Python下载安装及环境变量配置教程
- java map 迭代遍历_java 遍历Map的四种方式
- 晋升新一线的合肥,跨平台的.NET氛围究竟如何?
- Spark 某两个节点数据分析速度慢 - hbase数据删除(分裂) 元信息未删除导致 There is an overlap in the region chain.
- Thin的DateChooser代码学习(getScrollPosition)(原创,转载请声明)
- zabbix 客户端自定义端口监控
- Spark源码学习1.6——Executor.scala
- angular.injector()
- 盘点服装进销存软件和生产进销存软件排行榜
- 中国石油大学《画法几何》在线考试
- 香港、澳门通行证网上申请
- 『ML笔记』梯度下降法和随机梯度下降法和小批量梯度对比
- Factors of Factorial
- Qt 添加界面背景图片
- 解决module ‘mediapipe.python.solutions.face_mesh‘ has no attribute ‘FACE_CONNECTIONS‘问题的办法
- 夏日炎炎,使用决策树去挑选西瓜(python实现)
- 精选!必备的VSCode插件
- 发现一款好用到爆的数据库工具,被惊艳到了!
- 精彩剧透|SAP Concur 财经领袖峰会重磅嘉宾和深度议题
- 查看mysql的ibdata1_mysql 里的 ibdata1 文件
热门文章
- GitHub 被爆开始实名制,以便于执行美国贸易制裁
- java里面的string类型,java中的String类型(不知道理解的好不,请教大神)
- predict函数 R_RROC三剑客(一)使用R语言手撕ROC曲线
- java小数左右移_Java将小数位移至两倍
- 当前没有可用的服务器_调研Redis高可用两种方案
- ionic应用程序文件保存和清除缓存
- 金蝶kis仓库管理系统演示_金蝶KIS专业版生产管理的系统亮点功能
- 中兴计算机专业,中兴计算机专业面试题.pdf
- 周期置换加密算法用c语言实现,古典密码实验报告.doc
- android 仿美团悬浮,类似美团悬浮框的效果