我想知道是否有更简单的方式来执行java执行。延迟Java步骤执行
function(){
command 1;
thread.sleep();
command 2;
thread.sleep();
... and soo on
}
我想延迟我的功能中的每一步有没有更好的方法来做到这一点?
我想知道是否有更简单的方式来执行java执行。延迟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");
}
}
宇我真的很喜欢这个东西想要但我也想知道性能明智的是最好这样做,或选择旧的方法。 ? – 2012-02-09 06:07:34
@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
@kornero - 我认为慢方法调用在这里没有问题,我的意思是他想延迟它们... – 2012-02-09 11:53:11
您可以将这些命令放入数据结构(如List)中,并将该列表传递给执行该命令然后睡眠一段时间的方法。可能更优雅一些,但肯定不是更简单。
你也可以考虑将它们添加到一个定时器,但是,它再次优雅,但带来了很多机器,而不会提高你的功能。
你不说你为什么要这么做;可能有更好的方法来实现您的最终目标,而不是为您的代码添加一堆延迟。
如果这是需要在整个应用程序的大部分,我想看看外部施加的延迟机制:
如果这是为了调试,我会看看使用JPDA的StepRequest类型。
万一你想在生产代码中这样做,我会使用类似ASM的东西来转换目标字节代码。
我正在自动化几个我的测试用例使用Java和这些延迟我希望他们添加像thinktime的东西。 – 2012-02-10 07:03:19
速度测试:
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
非常感谢您的帮助帮助我深入了解了这一点。 – 2012-02-10 07:01:29
多么容易可以把它拿到可能? – 2012-02-06 12:28:18
我认为这是最干净的方式。你为什么不满意这个选项? – Saurabh 2012-02-06 12:32:42
您是否在Java EE容器中使用此标记,如标记所示? – Kris 2012-02-06 12:37:01