今天学的时候随手测了一下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方法没有被执行?的更多相关文章

  1. 创建线程时如果既传入了runnable对象,又继承thread重写了run方法,会执行的哪里的代码

    1 使用线程的方式,继承thread类,重写run方法 new Thread() { @Override public void run() { System.out.println("我是 ...

  2. 简单介绍一下python Queue中常用的方法

    Queue.qsize() 返回队列的大小 Queue.empty() 如果队列为空,返回True,反之False Queue.full() 如果队列满了,返回True,反之FalseQueue.fu ...

  3. Node.js的那些坑——如何让异步并发方法同步顺序执行(for循环+异步操作)

    1 前言 nodejs的回调,有时候真的是让人又爱又恨的,当需要用for循环把数据依次存入数据库,但是如果使用正常的for循环,永远都是最后一次值的记录,根本不符合要求. 解决此方案有几种,例如闭包( ...

  4. Python subprocess中的run方法

    调用subprocess的推荐方法是对于它可以处理的所有使用场景都使用run()函数. run()函数是在Python 3.5中添加的,如果在老版本中使用,需要下载并扩展. 扩展安装方式: $ pip ...

  5. Dart中的匿名方法与自执行方法

    void main() { // 匿名方法 var printSomethings = () { print("somethings"); }; printSomethings() ...

  6. Java -- Thread中start和run方法的区别

    一.认识Thread的 start() 和 run() 1.start(): 我们先来看看API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并发 ...

  7. 认识多线程中start和run方法的区别?

    一.认识多线程中的 start() 和 run() 1.start(): 先来看看Java API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并 ...

  8. 线程中start与run方法的主要区别

    区别一:                在于当程序调用start方法一个新线程将会被创建,并且在run方法中的代码将会在新线程上运行,                然而在你直接调用run方法的时候, ...

  9. JNI-Thread中start方法的调用与run方法的回调分析

    前言 在java编程中,线程Thread是我们经常使用的类.那么创建一个Thread的本质究竟是什么,本文就此问题作一个探索. 内容主要分为以下几个部分 1.JNI机制的使用 2.Thread创建线程 ...

随机推荐

  1. 4. python 修改字符串实例总结

    4. python 修改字符串实例总结 我们知道python里面字符串是不可原处直接修改的,为了是原来的字符串修改过来,我们有一下方法: 1.分片和合并 >>> a='abcde'  ...

  2. Oracle约束的启用和停用

      关于Oracle的约束概念和基本操作,我已经在以前的<Constraint基础概念>.<Constraint的简单操作>两篇文章中有过比较详细的介绍了,但是对于如何停用和启 ...

  3. Selenium2+python自动化46-js解决click失效问题

    前言 有时候元素明明已经找到了,运行也没报错,点击后页面没任何反应.这种问题遇到了,是比较头疼的,因为没任何报错,只是click事件失效了. 本篇用2种方法解决这种诡异的点击事件失效问题 一.遇到的问 ...

  4. json和gson的区别

    json是一种数据格式,便于数据传输.存储.交换gson是一种组件库,可以把java对象数据转换成json数据格式 GSON简单处理JSON json格式经常需要用到,google提供了一个处理jso ...

  5. HBase性能调优(转)

    原文链接:http://www.blogjava.net/ivanwan/archive/2011/06/15/352350.html 因官方Book Performance Tuning部分章节没有 ...

  6. Java操作Mongodb 保存/读取java对象到/从mongodb

    从http://central.maven.org/maven2/org/mongodb/mongo-java-driver/选择一个版本进行下载,这里选择的是3.0.0版本,具体下载以下jar包: ...

  7. iOS:多个单元格的删除(方法一)

    采用存取indexPath的方式,来对多个选中的单元格进行删除 删除前: 删除后: 分析:如何实现删除多个单元格呢?这需要用到UITableView的代理方法,即选中单元格时对单元格做的处理,同时我们 ...

  8. [转]局域网共享一键修复 18.5.8 https://zhuanlan.zhihu.com/p/24178142

    @echo offcolor 2fmode con cols=50 lines=30title OKShare [制作:wnsdt]ver | findstr "6.">nu ...

  9. 金蝶KIS下载地址

    升级方法: 您好,建议您先升级到标准版7.5,再升级到标准版8.1,直接用7.5的软件打开金蝶2000的账套,会提示升级,再用8.1的软件打开7.5的账套,升级前,需先备份账套. 金蝶KIS标准版和业 ...

  10. 十个WEB开发人员不可不知的HTML5工具

    Initializr 这是一个HTML5模板创建工具,帮助你得到持续的最新的HTML5样板文件. XRAY XRAY目前支持Safari, Firefox和IE浏览器,XRAY使用了CSS3的多个酷炫 ...