【问题标题】:Print out the time using currenttimemillis() function使用 currenttimemillis() 函数打印出时间
【发布时间】:2013-09-02 10:49:35
【问题描述】:

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

【问题讨论】:

  • 对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。

标签: java eclipse time


【解决方案1】:

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

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

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

【讨论】:

  • 错误信息:构造函数 TracingInvocationHandler (Object,PrintStream,Object) 未定义 // 错误仍然停留在同一行代码:(出于某种原因
  • @user2729154 然后传入一个 PrintStream :)
  • 无论我在做什么,我仍然收到错误消息:PirntStream 无法解析为变量.....我还包括这行代码:private static final PrintWriter PrintStream = null;错误仍然存​​在;)
  • Grr。 PrintStream 是一个类,而不是一个变量。您需要一个 PrintStream 的实例
  • :((((((((((((()我还是不能修复:((())
猜你喜欢
  • 2015-07-26
  • 1970-01-01
  • 2020-02-11
  • 2018-10-09
  • 1970-01-01
  • 2013-10-26
  • 2013-05-19
  • 2018-10-31
  • 1970-01-01
相关资源
最近更新 更多