我想知道是否有更简单的方法来延迟执行java。
function(){
command 1;
thread.sleep();
command 2;
thread.sleep();
... and soo on
}
我想延迟我的功能中的每一步是否有更好的方法来做到这一点?
答案 0 :(得分: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");
}
}
答案 1 :(得分:1)
您可以将命令放入数据结构(如List)中,并将列表传递给执行命令的方法,然后休眠一段时间。或许更优雅,但肯定不会更简单。
您也可以考虑将它们添加到计时器中,但同样,它更优雅,但却带来了相当多的机器而没有给您带来很多功能上的改进。
答案 2 :(得分:0)
你没有说你为什么要这样做;可能有更好的方法来实现您的最终目标,而不是为您的代码添加一堆延迟。
如果在应用程序的大部分内容都需要这样做,我会考虑在外部应用延迟机制:
如果这是用于调试,我会考虑使用JPDA的StepRequest类型。
如果您希望在生产代码中使用此代码,我会使用类似ASM的内容转换目标字节代码。
答案 3 :(得分: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