使用currenttimemillis()函数打印时间

时间:2013-09-02 10:49:35

标签: java eclipse time

任何机构都可以帮助理解为什么它不打印超时?  当我尝试加载图像时?例如,使用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)); 

1 个答案:

答案 0 :(得分:0)

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

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

用水和米饭煮饭的机器不能用水煮米饭。