繁体   English   中英

延迟Java步骤执行

[英]Delay Java Step Execution

我想知道是否有一种更简单的方法来处理Java执行。

function(){
  command 1;   
  thread.sleep();
  command 2;
  thread.sleep();
  ... and soo on
}

我想延迟功能的每一步,是否有更好的方法呢?

您可以使用类似的方法,但是我不是一个好主意。 我同意DaveHowes的观点 ,没有别的简单方法可以做到这一点。

package main;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class TempClass {
    public static void main(String[] args) throws ParseException, InterruptedException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        new TempClass().function();
    }

    private void function() throws NoSuchMethodException, InterruptedException, InvocationTargetException, IllegalAccessException {
        final Class aClass = this.getClass();
        List<Method> methods = new ArrayList<Method>() {{
            add(aClass.getDeclaredMethod("command1"));
            add(aClass.getDeclaredMethod("command2"));
        }};
        for (Method method : methods) {
            method.setAccessible(true);
            method.invoke(this);
            Thread.sleep(1000);
        }
    }

    private void command1() {
        System.out.println("command1");
    }

    private void command2() {
        System.out.println("command2");
    }
}

您可以将命令放入诸如List之类的数据结构中,并将该列表传递给执行该命令的方法,然后休眠一段时间。 也许更优雅一点,但肯定没有更简单。

您也可以考虑将它们添加到Timer中,但是,它再次更优雅,但是带来了很多机器,而没有在功能上进行很多改进。

您没有说为什么要这样做; 与在代码中添加大量延迟相比,实现最终目标可能是更好的方法。

如果整个应用程序的大部分都需要这样做,那么我将考虑在外部应用延迟机制:

  • 如果这是为了调试,我将考虑使用JPDAStepRequest类型。

  • 在极少数情况下,您可能希望在生产代码中使用ASM之类的东西来转换目标字节代码。

速度测试:

package test;

import net.sf.cglib.reflect.FastClass;
import net.sf.cglib.reflect.FastMethod;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class Test {

    private static final long SLEEP = 0;
    private static final Object[] withoutParameters = new Object[0];

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InterruptedException, IllegalAccessException {
        final Method[] methods = getMethods(Test.class, "command1", "command2");
        final FastMethod[] fastMethods = getFastMethods(Test.class, methods);
        final Test test = new Test();
        final int loop = 10* 1000 * 1000;

        long timeStamp = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            test.functionDirectCall();
        }
        System.out.println("DirectCall time:" + (System.currentTimeMillis() - timeStamp));

        timeStamp = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            test.functionMethod(methods);
        }
        System.out.println("Method time:" + (System.currentTimeMillis() - timeStamp));

        timeStamp = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            test.functionFastMethod(fastMethods);
        }
        System.out.println("FastMethod time:" + (System.currentTimeMillis() - timeStamp));
    }

    private void functionDirectCall() throws InterruptedException {
        this.command1();
        Thread.sleep(SLEEP);
        this.command2();
        Thread.sleep(SLEEP);
    }

    private void functionMethod(Method... methods) throws InvocationTargetException, IllegalAccessException, InterruptedException {
        for (Method method : methods) {
            method.invoke(this);
            Thread.sleep(SLEEP);
        }
    }

    private void functionFastMethod(FastMethod... fastMethods) throws InvocationTargetException, InterruptedException {
        for (FastMethod fastMethod : fastMethods) {
            fastMethod.invoke(this, withoutParameters);
            Thread.sleep(SLEEP);
        }
    }

    private static Method[] getMethods(final Class aClass, final String... methodNames) throws NoSuchMethodException {
        return new ArrayList<Method>() {{
            for (String methodName : methodNames) {
                final Method method = aClass.getDeclaredMethod(methodName);
                method.setAccessible(true);
                this.add(method);
            }
        }}.toArray(new Method[methodNames.length]);
    }

    private static FastMethod[] getFastMethods(final Class aClass, final Method... methods) {
        final FastClass fastClass = FastClass.create(aClass);
        return new ArrayList<FastMethod>() {{
            for (Method method : methods) {
                add(fastClass.getMethod(method));
            }
        }}.toArray(new FastMethod[methods.length]);
    }

    public void command1() throws InterruptedException {
        Thread.sleep(SLEEP);
    }

    public void command2() throws InterruptedException {
        Thread.sleep(SLEEP);
    }
}

输出:

DirectCall time:17615
Method time:19051
FastMethod time:17952

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM