Spring计时器StopWatch使用 StopWatch是位于org.springframework.util包下的一个工具类,通过它可方便的对程序部分代码进行计时(ms级别),适用于同步单线程代码块。 正常情况下,我们如果需要看某段代码的执行耗时,会通过如下的方式进行查看: publicstaticvoidmain(String[] args) throws InterruptedException{ StopWatchT...
import org.springframework.util.StopWatch;publicclassOTest{publicstaticvoidmain(String[] args) throws InterruptedException{ StopWatch stopWatch =newStopWatch();// 任务一模拟休眠3秒钟stopWatch.start("TaskOneName"); Thread.sleep(1000*3); System.out.println("当前任务名称:"+ stopWatch.currentTaskName...
如果使用了Spring框架,那么Spring已经提供了一个秒表工具StopWatch。 一、Java原生方式 这种方式最最简单,最好理解,经常会这么来写: public void test1() throws InterruptedException { long startTime = System.currentTimeMillis(); //获取开始时间 //函数主体代码 //... TimeUnit.SECONDS.sleep(1); long end...
package com.wfg.boot.spring; import org.springframework.util.StopWatch; import java.util.Arrays; /** * @author wufagang * @description * @date 2023年09月15日 20:48 */ public class StopWatchTest { public static void main(String[] args) throws InterruptedException { StopWatch stopWatch = ...
import org.springframework.util.StopWatch; public class StopWatchExample { public static void main(String[] args) throws InterruptedException { StopWatch stopWatch = new StopWatch(); stopWatch.start("Task 1"); Thread.sleep(1000); // 模拟耗时操作 stopWatch.stop(); stopWatch.start("Task 2"...
spring stopwatch 计时 spring定时器时间配置 首先,在springMVC的注解配置文件即启动servelet会读取的xml配置文件中添加定时器的规范 xmlns:task="http://www.springframework.org/schema/task" 和 http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.2.xsd...
当然了,除了选择 Spring 和 hutool 的 StopWatch,Apache commons-lang3 的 StopWatch 也是一个不错的可选项,更加灵活多变。StopWatch sw = StopWatch.createStarted();Thread.sleep(1000);System.out.printf("耗时:%dms.\n", sw.getTime());其他两个都是通过 new 来创建 StopWatch 对象,commons-lang3 ...
StopWatch需要依赖额外的Jar:commons-lang3或者spring-core,但因这两个Jar是Java开发中都必导的,因此依赖兼容性方面可以忽略 StopWatch有很多开源的框架都有提供类似的功能:比如Apache的commons-lang3,当然还有Spring framwork自己提供的,本文将针对此俩分别做介绍~ ...
importorg.springframework.util.StopWatch;publicclassStopWatchDemo{publicstaticvoidmain(String[]args)throwsInterruptedException{StopWatchclock=newStopWatch();clock.start("TaskOneName");Thread.sleep(1000*3);// 任务一模拟休眠3秒钟clock.stop();clock.start("TaskTwoName");Thread.sleep(1000*10);// 任务...
StopWatch是Spring框架提供的一个简单而强大的计时工具类,用于精确测量代码执行时间。它可以帮助开发者快速分析程序的性能瓶颈,从而优化代码,提高程序运行效率。StopWatch基于纳秒级别的时间计算,支持多个任务的计时,并且可以方便地输出计时结果。 二、StopWatch基本功能 开始/停止计时:StopWatch提供了开始和停止计时的方法,...