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创建线程 ...
随机推荐
- 深入浅出VC++串口编程之基于Win32 API
1.API描述 在WIN32 API中,串口使用文件方式进行访问,其操作的API基本上与文件操作的API一致. 打开串口 Win32 中用于打开串口的API 函数为CreateFile,其原型为: H ...
- 转: IOS程序内发短信 MFMessageComposeViewController
文章转载地址:http://www.headsky.org/?p=63 iOS4.0新加入了MFMessageComposeViewController和MFMessageComposeViewCon ...
- sql递归查询子级
WITH T(emp_no, name, dept_no, the_level, path,path1,manager_id) AS( SELECT emp_no, name, dept_no ,1 ...
- Python3.6学习笔记(六)
WSGI Python Web Server Gateway Interface 规范学习 由于Python的灵活性,提供了多种方式可以作为服务端语言,包括Python编写的服务器(Medusa).P ...
- Mysql中与时间相关的统计分析
最近项目需要统计一段日期范围内,根据每分钟.几分钟.每天分别统计汇总某些事件/指标的发生总次数,平均发生次数,因此总结了Mysql中与时间处理.统计相关的资料. 按分钟统计某一时间段内的数据 SELE ...
- MyBatis+Spring SQL效率测试报告
1. 数据库结构 2. insert 测试 insert 的测试包括 1) 批量拼接values()插入 2) 有事务for循环插入 3) 无事务for循环插入 测试 SQL: <!-- 普通 ...
- 基于at91rm9200的i2c分析(DS1307实时时钟芯片)
board-ek.c 构造i2c_board_info结构体 static struct i2c_board_info __initdata ek_i2c_devices[] = { { ...
- C# WinForm开发系列 - GDI+【转】
http://blog.csdn.net/blue_sky6/article/details/53811435?locationNum=6&fps=1 C# WinForm开发系列 - GDI ...
- iOS开发-数据选择UIPickerView
UIPickerView开发一般选择区域或者分级数据的时候会使用到,类似于前端中用到树状结构,不过PC上一般都是从上到下的分级,使用UIPickView是从左到右实现,可以动态的设置UIPickVie ...
- EventBus 事件总线 原理 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...