360建筑网密码忘了,如何设定旅游网站seo核心关键词,平面设计手机作图软件,做外贸需要自己建网站吗推荐阅读#xff1a;Sping源码RedisNginxMySQL等七篇实战技术文档#xff0c;阿里大佬推荐阿里内部#xff1a;2020年全技术栈文档PPT分享#xff0c;(万粉总结#xff0c;回馈粉丝)在我们的实际开发中#xff0c;多多少少会遇到统计一段代码片段的耗时的情况#xff0c…推荐阅读Sping源码RedisNginxMySQL等七篇实战技术文档阿里大佬推荐阿里内部2020年全技术栈文档PPT分享(万粉总结回馈粉丝)在我们的实际开发中多多少少会遇到统计一段代码片段的耗时的情况我们一般的写法如下long start System.currentTimeMillis();try { // .... 具体的代码段} finally { System.out.println(cost: (System.currentTimeMillis() - start));}上面的写法没有什么毛病但是看起来就不太美观了那么有没有什么更优雅的写法呢1. 代理方式了解 Spring AOP 的同学可能立马会想到一个解决方法如果想要统计某个方法耗时使用切面可以无侵入的实现如// 定义切点拦截所有满足条件的方法Pointcut(execution(public * com.git.hui.boot.aop.demo.*.*(*)))public void point() {}Around(point())public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { long start System.currentTimeMillis(); try{ return joinPoint.proceed(); } finally { System.out.println(cost: (System.currentTimeMillis() - start)); }}Spring AOP 的底层支持原理为代理模式为目标对象提供增强功能在 Spring 的生态体系下使用 aop 的方式来统计方法耗时可以说少侵入且实现简单但是有以下几个问题统计粒度为方法级别类内部方法调用无法生效(详情可以参考博文【SpringBoot 基础系列教程】AOP 之高级使用技能)2. AutoCloseable在 JDK1.7 引入了一个新的接口AutoCloseable, 通常它的实现类配合try{}使用可在 IO 流的使用上经常可以看到下面这种写法// 读取文件内容并输出try (Reader stream new BufferedReader(new InputStreamReader(new FileInputStream(/tmp)))) { List list ((BufferedReader) stream).lines().collect(Collectors.toList()); System.out.println(list);} catch (IOException e) { e.printStackTrace();}注意上面的写法中最值得关注一点是不需要再主动的写stream.close了主要原因就是在try(){}执行完毕之后会调用方法AutoCloseable#close方法基于此我们就会有一个大单的想法下一个Cost类实现AutoCloseable接口创建时记录一个时间close 方法中记录一个时间并输出时间差值将需要统计耗时的逻辑放入try(){}代码块下面是一个具体的实现public static class Cost implements AutoCloseable { private long start; public Cost() { this.start System.currentTimeMillis(); } Override public void close() { System.out.println(cost: (System.currentTimeMillis() - start)); }}public static void testPrint() { for (int i 0; i 5; i) { System.out.println(now i); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } }}public static void main(String[] args) { try (Cost c new Cost()) { testPrint(); } System.out.println(------over-------);}执行后输出如下:now 0now 1now 2now 3now 4cost: 55如果代码块抛异常也会正常输出耗时么public static void testPrint() { for (int i 0; i 5; i) { System.out.println(now i); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } if (i 3) { throw new RuntimeException(some exception!); } }}再次输出如下并没有问题now 0now 1now 2now 3cost: 46Exception in thread main java.lang.RuntimeException: some exception!at com.git.hui.boot.order.Application.testPrint(Application.java:43)at com.git.hui.boot.order.Application.main(Application.java:50)3. 小结除了上面介绍的两种方式还有一种在业务开发中不太常见但是在中间件、偏基础服务的功能组件中可以看到利用 Java Agent 探针技术来实现比如阿里的 arthas 就是在 JavaAgent 的基础上做了各种上天的功能后续介绍 java 探针技术时会专门介绍下面小结一下三种统计耗时的方式基本写法long start System.currentTimeMillis();try { // .... 具体的代码段} finally { System.out.println(cost: (System.currentTimeMillis() - start));}优点是简单适用范围广泛缺点是侵入性强大量的重复代码Spring AOP在 Spring 生态下可以借助 AOP 来拦截目标方法统计耗时Around(...)public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { long start System.currentTimeMillis(); try{ return joinPoint.proceed(); } finally { System.out.println(cost: (System.currentTimeMillis() - start)); }}优点无侵入适合统一管理(比如测试环境输出统计耗时生产环境不输出)缺点是适用范围小且粒度为方法级别并受限于 AOP 的使用范围AutoCloseable这种方式可以看做是第一种写法的进阶版// 定义类public static class Cost implements AutoCloseable { private long start; public Cost() { this.start System.currentTimeMillis(); } Override public void close() { System.out.println(cost: (System.currentTimeMillis() - start)); }}// 使用姿势try (Cost c new Cost()) { ...}优点是简单适用范围广泛且适合统一管理缺点是依然有代码侵入说明上面第二种方法看着属于最优雅的方式但是限制性强如果有更灵活的需求建议考虑第三种写法在代码的简洁性和统一管理上都要优雅很多相比较第一种可以减少大量冗余代码作者一灰灰链接https://juejin.im/post/5e5e4cc6518825493d6a96c4