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创建线程 ...
随机推荐
- 用最简单的例子理解命令模式(Command Pattern)
假设想让遥控器控制电灯的开关.电视机的开关和切换,该如何做? 所有的开.关.切换都是遥控器发出的指令,把这些指令统一抽象成一个接口. public interface IControl { void ...
- cocos2d-x动画加速与减速
动画是游戏的必定要素之中的一个,在整个游戏过程中,又有着加速.减速动画的需求.以塔防为样例,布塔的时候希望可以将游戏减速,布好塔后,则希望能将游戏加速:当某个怪被冰冻后,移动速度减缓,而其它怪的移动速 ...
- jquery实现上线翻滚效果公告
//样式文件: <style type="text/css"> * { margin:; padding:; } .scrollNews { width: 100%; ...
- Coursera课程《Python数据结构》中课程目录
Python Data Structures Python Data Structures is the second course in the specialization Python for ...
- 深度学习材料:从感知机到深度网络A Deep Learning Tutorial: From Perceptrons to Deep Networks
In recent years, there’s been a resurgence in the field of Artificial Intelligence. It’s spread beyo ...
- kafka存储机制
kafka存储机制 @(博客文章)[storm|大数据] kafka存储机制 一关键术语 二topic中partition存储分布 三 partiton中文件存储方式 四 partiton中segme ...
- 我所遭遇过的游戏中间件---Redux
我所遭遇过的游戏中间件---Redux 一.关于Redux Substance Redux 是一款纹理处理软件加中间件,专门用于纹理生成和压缩.具其用户指南介绍,它能够对纹理集进行优化,可以将现有压缩 ...
- C/C++中printf/cout 计算顺序与缓冲区问题
1.printf/cout在同一个语句中都是从右向左计算的. 看如下的代码: #include <stdio.h> int main() { ; printf("%d %d&qu ...
- 泛型 Generic 类型擦除引起的问题及解决方法
参考:http://blog.csdn.net/lonelyroamer/article/details/7868820#comments 因为种种原因,Java不能实现真正的泛型,只能使用类型擦除来 ...
- jquery中filter(fn)的使用研究
jquery中filter(fn)给出的官方说明是: 筛选出与指定函数返回值匹配的元素集合 这个函数内部将对每个对象计算一次 (正如 '$.each'). 如果调用的函数返回false则这个元素被删除 ...