gpt4 book ai didi

java - (Java使用eclipse)currentTimeMillis()

转载 作者:行者123 更新时间:2023-12-01 23:36:59 24 4
gpt4 key购买 nike

这只是我目前正在工作的一部分,我想使用 currentTimeMillis() 打印要加载图像的时间,有什么原因导致它不起作用?

package method;

import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Date;

public class TracingInvocationHandler implements InvocationHandler {

private Object target;
private PrintWriter out;

public TracingInvocationHandler(Object target, PrintWriter out) {
this.target = target;
this.out = out;

}

@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
long startTime = System.currentTimeMillis();
Object result = null;
out.println("Image " + method.getName() + " (...) entered.");
result = method.invoke(target, args);
out.println("Image " + method.getName() + " (...) returned.");
long endTime = System.currentTimeMillis();
System.out.printf(" [%s] %s Image %s took %d ms:",new Date().toString(), method.getName(),args[0], (endTime - startTime) + "ms");
return result;

}

public static Object createProxy(Object target, PrintWriter out) {
Class<?> targetClass = target.getClass();
ClassLoader currentClassLoader = targetClass.getClassLoader();
Class<?>[] interfaces = targetClass.getInterfaces();
InvocationHandler handler = new TracingInvocationHandler(target, out);
return Proxy.newProxyInstance(currentClassLoader, interfaces, handler);
}

最佳答案

(endTime - startTime) + "ms" 中删除 + "ms"。模式中相应的格式 %d 需要一个数字对象。 (endTime - startTime) + "ms" 生成一个 String

您可能收到异常。下次您提出问题时,请包括在内。这次你很幸运。

关于java - (Java使用eclipse)currentTimeMillis(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18541485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com