Spring StopWatch详解

简介: 《读尽源码》
  • 全路径: org.springframework.util.StopWatch

属性

  • taskList: 任务信息列表
  • keepTaskList: 是否保留任务信息列表
  • startTimeMillis: 任务开始的时间
  • currentTaskName: 任务名称
  • lastTaskInfo: 任务信息
  • taskCount: 任务数量
  • totalTimeMillis: 总共花费的时间

方法

  • org.springframework.util.StopWatch.start(java.lang.String)
public void start(String taskName) throws IllegalStateException {
        if (this.currentTaskName != null) {
            throw new IllegalStateException("Can't start StopWatch: it's already running");
        }
        this.currentTaskName = taskName;
        this.startTimeMillis = System.currentTimeMillis();
    }Copy to clipboardErrorCopied
  • org.springframework.util.StopWatch.stop
public void stop() throws IllegalStateException {
        if (this.currentTaskName == null) {
            throw new IllegalStateException("Can't stop StopWatch: it's not running");
        }
        // 消费的时间
        long lastTime = System.currentTimeMillis() - this.startTimeMillis;
        this.totalTimeMillis += lastTime;
        // 任务信息初始化
        this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
        if (this.keepTaskList) {
            this.taskList.add(this.lastTaskInfo);
        }
        ++this.taskCount;
        this.currentTaskName = null;
    }
相关文章
|
Java Apache Spring
Apache和Spring提供的StopWatch执行时间监视器
Apache和Spring提供的StopWatch执行时间监视器前言编码过程中我们经常会希望得到一段代码(一个方法)的执行时间,本文将介绍两种时间监视器(秒表)来让你优雅的、灵活的处理这个问题。 Java源生方式这种方式最最简单,最好理解,当然也是最为常用:我们自己书写。
1397 0
|
监控 Java Spring
spring StopWatch监控请求执行时间
spring StopWatch监控请求执行时间
315 0
|
监控 Java Apache
【小家java】Apache Commons-lang3提供的StopWatch执行时间监视器,以及Spring提供的StopWatch分析(下)
【小家java】Apache Commons-lang3提供的StopWatch执行时间监视器,以及Spring提供的StopWatch分析(下)
|
Java Apache Spring
【小家java】Apache Commons-lang3提供的StopWatch执行时间监视器,以及Spring提供的StopWatch分析(上)
【小家java】Apache Commons-lang3提供的StopWatch执行时间监视器,以及Spring提供的StopWatch分析(上)
|
6月前
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
1020 0
|
7月前
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
769 0
|
3月前
|
JavaScript Java Maven
【SpringBoot(二)】带你认识Yaml配置文件类型、SpringMVC的资源访问路径 和 静态资源配置的原理!
SpringBoot专栏第二章,从本章开始正式进入SpringBoot的WEB阶段开发,本章先带你认识yaml配置文件和资源的路径配置原理,以方便在后面的文章中打下基础
368 3
|
3月前
|
Java 测试技术 数据库连接
【SpringBoot(四)】还不懂文件上传?JUnit使用?本文带你了解SpringBoot的文件上传、异常处理、组件注入等知识!并且带你领悟JUnit单元测试的使用!
Spring专栏第四章,本文带你上手 SpringBoot 的文件上传、异常处理、组件注入等功能 并且为你演示Junit5的基础上手体验
904 2
|
10月前
|
前端开发 Java 数据库
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 介绍
本课介绍Spring Boot集成Thymeleaf模板引擎。Thymeleaf是一款现代服务器端Java模板引擎,支持Web和独立环境,可实现自然模板开发,便于团队协作。与传统JSP不同,Thymeleaf模板可以直接在浏览器中打开,方便前端人员查看静态原型。通过在HTML标签中添加扩展属性(如`th:text`),Thymeleaf能够在服务运行时动态替换内容,展示数据库中的数据,同时兼容静态页面展示,为开发带来灵活性和便利性。
439 0