2012-02-06 279 views
0

我想知道是否有更簡單的方式來執行java執行。延遲Java步驟執行

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

我想延遲我的功能中的每一步有沒有更好的方法來做到這一點?

+0

多麼容易可以把它拿到可能? – 2012-02-06 12:28:18

+0

我認爲這是最乾淨的方式。你爲什麼不滿意這個選項? – Saurabh 2012-02-06 12:32:42

+0

您是否在Java EE容器中使用此標記,如標記所示? – Kris 2012-02-06 12:37:01

回答

2

你可以用這樣的東西,但我不是個好主意。 我同意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"); 
    } 
} 
+0

宇我真的很喜歡這個東西想要但我也想知道性能明智的是最好這樣做,或選擇舊的方法。 ? – 2012-02-09 06:07:34

+1

@DanielEuchar使用反射調用方法比直接調用要慢,但是可以使用「cglib」庫中的FastMethod類而不是Method類。 Standart java反射使用JNI調用(http://en.wikipedia.org/wiki/Java_Native_Interface),cglib的工作速度更快。我可以在這裏找到它:http://cglib.sourceforge.net/ – kornero 2012-02-09 10:44:34

+0

@kornero - 我認爲慢方法調用在這裏沒有問題,我的意思是他想延遲它們... – 2012-02-09 11:53:11

1

您可以將這些命令放入數據結構(如List)中,並將該列表傳遞給執行該命令然後睡眠一段時間的方法。可能更優雅一些,但肯定不是更簡單。

你也可以考慮將它們添加到一個定時器,但是,它再次優雅,但帶來了很多機器,而不會提高你的功能。

0

你不說你爲什麼要這麼做;可能有更好的方法來實現您的最終目標,而不是爲您的代碼添加一堆延遲。

如果這是需要在整個應用程序的大部分,我想看看外部施加的延遲機制:

  • 如果這是爲了調試,我會看看使用JPDAStepRequest類型。

  • 萬一你想在生產代碼中這樣做,我會使用類似ASM的東西來轉換目標字節代碼。

+0

我正在自動化幾個我的測試用例使用Java和這些延遲我希望他們添加像thinktime的東西。 – 2012-02-10 07:03:19

0

速度測試:

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 
+0

非常感謝您的幫助幫助我深入瞭解了這一點。 – 2012-02-10 07:01:29