今天学的时候随手测了一下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. 我对NHibernate的感受(4):令人欣喜的Interceptor机制

    之前谈了NHibernate的几个方面,似乎抱怨的居多,不过这次我想谈一下我对Interceptor的感受,则基本上都是好话了.这并不一定是说Interceptor设计的又多么好(事实上它使用起来还是 ...

  2. 转 Objective-C中不同方式实现锁(一)

    为什么需要使用锁,当然熟悉多线程的你,自然不会对它觉得陌生. 那你在代码中是否很好的使用了锁的机制呢?你又知道几种实现锁的方法呢? 今天一起来探讨一下Objective-C中几种不同方式实现的锁,在这 ...

  3. 学习 KJFrameForAndroid

    简介 KJFrameForAndroid 又叫KJLibrary,是一个android的orm 和 ioc 框架.同时封装了android中的Bitmap与Http操作的框架,使其更加简单易用:KJF ...

  4. jaxb使用

    一.前言 JAXB——Java Architecture for XML Binding,是一项可以根据XML Schema产生Java类的技术.JAXB提供将XML实例文档反向生成Java对象树的方 ...

  5. 《阿里巴巴JAVA开发手册》里面写超过三张表禁止join这是为什么?

    分库分页.应用里做join 多表join性能很差 参考: 1.https://www.zhihu.com/question/56236190

  6. 我所遭遇过的游戏中间件---SpeedTree

    我所遭遇过的游戏中间件---SpeedTree SpeedTree是一个专门用于渲染植被的中间件,并提供了一套完善的植物编辑工具.在它官方提供的DEMO中,你会看到高度逼真的树木和植物,在风的影响下树 ...

  7. IOS中键盘隐藏几种方式

    在ios开发中,经常需要输入信息.输入信息有两种方式: UITextField和UITextView.信息输入完成后,需要隐藏键盘,下面为大家介绍几种隐藏键盘的方式. <一> 点击键盘上的 ...

  8. statickeyword

    static词义:静态的,可以用于修饰变量和方法,static方法块可以优先于构造函数运行. 被static修饰的变量,叫静态变量,静态变量在内存中仅仅有一份拷贝 public static Stri ...

  9. 【LeetCode】Path Sum II 二叉树递归

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  10. Untracked Files Prevent Checkout move or commit them before checkout

    点开View Files... 查看里面的文件名称,在项目的.idea文件夹中删掉ViewFiles显示的文件夹名称就好