gpt4 book ai didi

java - 使用 currenttimemillis() 函数打印出时间

转载 作者:行者123 更新时间:2023-12-01 04:25:07 26 4
gpt4 key购买 nike

任何人都可以帮忙解释为什么它不打印时间吗? 当我尝试加载图像时?例如使用 java eclipse..currenttimemillis() 加载图像需要多长时间 我想提前向您表示衷心的感谢

     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;
private Object obj;

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

}

@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(obj, args);
out.println("Image " + method.getName() + " (...) returned.");

long endTime = System.currentTimeMillis();

long totalTime = endTime - startTime;

String strTotalTime = String.valueOf(totalTime);

System.out.printf(" [%s] %s Image %s took %d ms:",new Date().toString(), method.getName(),args[0],strTotalTime);

return result;

}

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

public static Object newInstance(Object obj){ //create a new instance
ClassLoader loader = obj.getClass().getClassLoader();
Class[] classes = obj.getClass().getInterfaces();
return Proxy.newProxyInstance(
loader, classes, new TracingInvocationHandler(obj));

}

}

  error message: the constructor TracingInvocationHanlder(Object) is undefined
error message at last line : new TracingInvocationHandler(obj));

最佳答案

您永远不会为 TracingIncationHandler 传递正确的参数。确保传递完整参数,例如:

new TracingInvocationHandler(obj, System.out, obj2);

用水和米来煮饭的机器只用水是无法煮出米饭的。

关于java - 使用 currenttimemillis() 函数打印出时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18571411/

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