AOP切面之打印方法时间
在看线程并发的书籍时看到ThreadLocal,利用线程变量打印方法执行时间,联想到可以用aop实现全局方法打印 下面先看单独使用ThreadLocal打印的方法 public class profiler { //第一次get()方法调用时会进行初始化(如果set方法没有调用),每个线程会调用一次 private static final ThreadLocal<Long> TimeThreadLocal = new ThreadLocal<Long>() { protected Long initialValue() { return System.currentTimeMillis(); } }; public static final void begin() { TimeThreadLocal.set(System.currentTimeMillis()); } public static final Long end() { return System.currentTimeMillis() - TimeThreadLocal.get(); } public static void main(String[] args) throws InterruptedException { profiler.begin(); TimeUnit.SECONDS.sleep(1);