[英]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中,但是,它再次更優雅,但是帶來了很多機器,而沒有在功能上進行很多改進。
您沒有說為什么要這樣做; 與在代碼中添加大量延遲相比,實現最終目標可能是更好的方法。
如果整個應用程序的大部分都需要這樣做,那么我將考慮在外部應用延遲機制:
如果這是為了調試,我將考慮使用JPDA的StepRequest類型。
在極少數情況下,您可能希望在生產代碼中使用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.