Hystrix的一个坑,queue中的run方法没有被执行?
今天学的时候随手测了一下Hystrix的queue的异步执行,发现执行queue之后,还没有打印run方法中的内容,程序就结束了:
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; /**
* Created by liu.yuxiang on 2017/10/10.
*/
public class UserCommand extends HystrixCommand<String> {
private String name;
protected UserCommand(Setter setter,String name) {
super(setter);
this.name=name;
} public String run() throws InterruptedException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(int i=0;i<3;i++){
Thread.sleep(1000l);
Date d = new Date();
System.out.println(sdf.format(d)+"休息一秒后打印--"+i+":hello "+name);
}
return "hello "+name;
} public static void main(String[] args) throws Exception { UserCommand userCommand = new UserCommand(
Setter.withGroupKey(
HystrixCommandGroupKey.Factory.asKey("")
).andCommandPropertiesDefaults(
HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(50000)
),"tester"); Future<String> f = userCommand.queue();
new Thread(){
public void run(){
for(int i=0;i<5;i++){
System.out.println("t1-"+i);
}
}
}.start();
String result = null;
//result = f.get();
System.out.println("finaly:"+result);
}
}
其实queue还是异步执行的,只不过使用queue创建的是一个 【守护线程】,该线程还没来得及执行,主线程就已经结束了,改成以下形式就能看出来:
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; /**
* Created by liu.yuxiang on 2017/10/10.
*/
public class UserCommand extends HystrixCommand<String> {
private String name;
protected UserCommand(Setter setter,String name) {
super(setter);
this.name=name;
} public String run() throws InterruptedException {
System.out.println("im in");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(int i=0;i<3;i++){
Thread.sleep(1000l);
Date d = new Date();
System.out.println(sdf.format(d)+"休息一秒后打印--"+i+":hello "+name);
}
return "hello "+name;
} public static void main(String[] args) throws Exception { UserCommand userCommand = new UserCommand(
Setter.withGroupKey(
HystrixCommandGroupKey.Factory.asKey("")
).andCommandPropertiesDefaults(
HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(50000)
),"tester"); Future<String> f = userCommand.queue();
new Thread(){
public void run(){
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0;i<5;i++){
System.out.println("t1-"+i);
}
}
}.start();
String result = null;
//result = f.get();
System.out.println("finaly:"+result);
}
}
run中的方法只来得及执行第一句。
Hystrix的一个坑,queue中的run方法没有被执行?的更多相关文章
- 创建线程时如果既传入了runnable对象,又继承thread重写了run方法,会执行的哪里的代码
1 使用线程的方式,继承thread类,重写run方法 new Thread() { @Override public void run() { System.out.println("我是 ...
- 简单介绍一下python Queue中常用的方法
Queue.qsize() 返回队列的大小 Queue.empty() 如果队列为空,返回True,反之False Queue.full() 如果队列满了,返回True,反之FalseQueue.fu ...
- Node.js的那些坑——如何让异步并发方法同步顺序执行(for循环+异步操作)
1 前言 nodejs的回调,有时候真的是让人又爱又恨的,当需要用for循环把数据依次存入数据库,但是如果使用正常的for循环,永远都是最后一次值的记录,根本不符合要求. 解决此方案有几种,例如闭包( ...
- Python subprocess中的run方法
调用subprocess的推荐方法是对于它可以处理的所有使用场景都使用run()函数. run()函数是在Python 3.5中添加的,如果在老版本中使用,需要下载并扩展. 扩展安装方式: $ pip ...
- Dart中的匿名方法与自执行方法
void main() { // 匿名方法 var printSomethings = () { print("somethings"); }; printSomethings() ...
- Java -- Thread中start和run方法的区别
一.认识Thread的 start() 和 run() 1.start(): 我们先来看看API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并发 ...
- 认识多线程中start和run方法的区别?
一.认识多线程中的 start() 和 run() 1.start(): 先来看看Java API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并 ...
- 线程中start与run方法的主要区别
区别一: 在于当程序调用start方法一个新线程将会被创建,并且在run方法中的代码将会在新线程上运行, 然而在你直接调用run方法的时候, ...
- JNI-Thread中start方法的调用与run方法的回调分析
前言 在java编程中,线程Thread是我们经常使用的类.那么创建一个Thread的本质究竟是什么,本文就此问题作一个探索. 内容主要分为以下几个部分 1.JNI机制的使用 2.Thread创建线程 ...
随机推荐
- MEF(Managed Extensibility Framework)有选择性地使用扩展组件
在"MEF(Managed Extensibility Framework)使用全部扩展组件"中,客户端应用程序调用了所有的扩展组件,而且如果有新的扩展组件加入,必须先关闭程序,再 ...
- MVC对集合筛选,不使用Where(),而使用FindAll()
当想对集合筛选的时候,经常想到用Where过滤,而实际上List<T>.FindAll()也是不错的选择. 如果有一个订单,属性有下单时间.区域等等.如何使用List<T>.F ...
- 页面中checkbox返回的是一个数组,如何对数组进行操作
1. 仅仅利用javascript进行操作: //html代码如下: <form action="#" method="POST" onsubmit=&q ...
- Tomcat集群扩展session集中管理,Memcached-session-manager使用
研究tomcat做负载均衡的时候如何实现ha,还有就是不采用session复制的方法做集群. 想到的是将session全部存储在后端的缓存服务器中. 正好网上有这么一个工具Memcached-sess ...
- pom-4.0.0.xml中心仓库
<!--Licensed to the Apache Software Foundation (ASF) under oneor more contributor license agreeme ...
- Unity3d-Particle System系统的学习(三)
这节课我们来实战下上几节讲的几乎所有Particle System用到的参数. 我们今天制作下图所示的粒子: 类似于带有光晕的魔法球.用到的材质也就是上节课用到的材质贴图. http://pan.ba ...
- CoreAudio实现录音播音和扬声器听筒模式的切换
本例子使用Core Audio实现类似于微信的音频对讲功能,可以录音和播放并且实现了听筒模式和扬声器模式的切换.录音主要使用AVAudioRecorder类来实现录音功能,播放则使用AVAudioPl ...
- Informatica 常用组件Source Qualifier之九 创建SQ转换
可以配置 Designer 在您将源拖到映射中时默认创建源限定符转换,您也可以手动创建源限定符转换. 默认创建源限定符转换 可以配置 Designer 在您将源拖到映射中时自动创建一个源限定符转换. ...
- 实现iframe窗口高度自适应的又一个巧妙思路
domainA 中有一个页面index.html,通过iframe嵌套了domainB中的一个页面other.html由于other.html页面在iframe中显示,而且其页面内容会动态的增加或减少 ...
- 查询分页-----强势top
查询分页:语句1性能提升10倍多,仅仅是由于多了个topkeyword,非常不理解啊!!!! 1.查询时间1s内,r_object_id主键 select top 100 * from ( sele ...