java 中stopwatch,Stopwatch

java 中stopwatch,Stopwatch/***Anobjectthataccuratelymeasureselapsedtime:themeasureddurationbetweentwo*successivereadingsof”now”inthesameprocess.*一个精确计算消耗时间的对象:计算在同一进程中两次连续读取当前时间之间持续的时间*Incontrast,wallti…

大家好,又见面了,我是你们的朋友全栈君。

/**

* An object that accurately measures elapsed time: the measured duration between two

* successive readings of “now” in the same process.

*一个精确计算消耗时间的对象:计算在同一进程中两次连续读取当前时间之间持续的时间

*

In contrast, wall time is a reading of “now” as given by a method like

* {

@link System#currentTimeMillis()}, best represented as an {

@link Instant}. Such values

* can be subtracted to obtain a {

@code Duration} (such as by {

@code Duration.between}), but

* doing so does not give a reliable measurement of elapsed time, because wall time readings

* are inherently approximate, routinely affected by periodic clock corrections. Because this class

* (by default) uses {

@link System#nanoTime}, it is unaffected by these changes.

*作为对照,实际时间是读取当前时间,就像System.currentTimeMillis()方法,一瞬间的最好代表。这种值相减可以得到一段时间,但是这样得不到准确的消耗的时间,因为实际时间读取是本质上近似的,

经常受到周期性时钟校正的影响。因为这个类使用System.nanoTime方法,它不受这些变化的影响

*

Use this class instead of direct calls to {@link System#nanoTime} for two reasons:

*用这个类代替直接调用System.nanoTime有两个原因

*

*

The raw {@code long} values returned by {@code nanoTime} are meaningless and unsafe to use

* in any other way than how {@code Stopwatch} uses them.

任何地方使用nanoTime返回的这个原生的long类型的值相对于Stopwatch使用它相比,毫无意义而且不安全

*

An alternative source of nanosecond ticks can be substituted, for example for testing or

* performance reasons, without affecting most of your code.

一个可供选择的资源是时间片段可以被替换,例如为了测试或性能原因,不影响你的大部分代码

*

*

*

Basic usage:

*

*

{@code

* Stopwatch stopwatch = Stopwatch.createStarted();

* doSomething();

* stopwatch.stop(); // optional

*

* Duration duration = stopwatch.elapsed();

*

* log.info(“time: ” + stopwatch); // formatted string like “12.3 ms”

* }

*

*

The state-changing methods are not idempotent; it is an error to start or stop a stopwatch

* that is already in the desired state.

*这个状态更改的方法不是幂等的,开始或停止一个已经处于期望状态的stopwatch对象是一个错误。

*

When testing code that uses this class, use {@link #createUnstarted(Ticker)} or {@link

* #createStarted(Ticker)} to supply a fake or mock ticker. This allows you to simulate any valid

* behavior of the stopwatch.

*当测试代码使用这个类,使用createUnstarted(Ticker)或createStarted(Ticker)提供一个假的或模拟的ticker,这里允许你模拟任何stopwatch有效的行为

*

Note: This class is not thread-safe.

*这个类是非线程安全的。

*

Warning for Android users: a stopwatch with default behavior may not continue to keep

* time while the device is asleep. Instead, create one like this:

*安卓用户的警告:设备休眠的时候stopwatch默认可能不会继续计算。代替的创建一个类似这种的代码:

*

{@code

* Stopwatch.createStarted(

* new Ticker() {

* public long read() {

* return android.os.SystemClock.elapsedRealtimeNanos();

* }

* });

* }

*

* @author Kevin Bourrillion

* @since 10.0

*/

总结:stopwatch主要功能就是两次star和stop调用之间的时间,还可以多次累加、可以重置。非线程安全的。

增加ticker的作用是可以自己替换和实现时间的读取。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/152409.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java 泛型擦除_java中泛型的使用

    Java 泛型擦除_java中泛型的使用java泛型的残酷现实就是:在泛型代码内部,无法获得任何有关泛型参数类型的信息。在使用泛型时,任何具体的类型都被擦除,唯一知道的是你在使用一个对象。比如:List<String>和List<Integer>在运行事实上是相同的类型。他们都被擦除成他们的原生类型,即List。snippet1:1packagecom.cognizant.ch15…

    2022年9月25日
    2
  • mac mysql改密码_mac系统重置密码

    mac mysql改密码_mac系统重置密码MAC重置MySql密码步骤:1.关闭mysql服务2.打开终端按步骤输入:输入1:cd/usr/local/mysql/bin/输入2:sudo./mysqld_safe–skip-grant-tables3.打开另外一个终端窗口:第一步输入:cd/usr/local/mysql/bin/第二步输入:./mysql第三步输入:FLUSHPRIVILEGES;第四步输入:ALTERUSER‘root’@‘localhost’IDENTIFIEDBY‘1

    2022年10月11日
    5
  • c语言学生成绩管理系统_学生成绩管理系统c++代码结构体

    c语言学生成绩管理系统_学生成绩管理系统c++代码结构体c语言管理系统牛~~/*引用库函数*/#include<stdio.h>#include<stdlib.h>#include<string.h>/*定义结构体数组*/typedefstruct{charnum[12];/*学号*/charname[20];/*姓名*/charsex[2];/*性别*/intscore[3];/*成绩*/

    2025年11月20日
    3
  • kafka队列模式_redis消息队列和mq

    kafka队列模式_redis消息队列和mq一、消息队列概述消息队列中间件是分布式系统中重要的组件,主要解决应用解耦,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构。目前使用较多的消息队列有ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ二、消息队列应用场景以下介绍消息队列在实际应用中常用的使用场景。异步处理,应用解耦,流量削锋和消息通讯四个场景。2.1异步处理场景说明:用户注册后,需要发注册邮件和注册短信。传统的做法有两种1.串行的方式;2.并行方式a、串行方式:将注册

    2022年10月9日
    3
  • vueThink权限配置

    vueThink权限配置

    2021年10月11日
    48
  • 十大经典排序算法详解(三)-堆排序,计数排序,桶排序,基数排序[通俗易懂]

    十大经典排序算法详解(三)-堆排序,计数排序,桶排序,基数排序[通俗易懂]十大经典排序算法-堆排序,计数排序,桶排序,基数排序1-堆排序算法思想:算法图解:示例代码:在这里插入代码片复杂度分析:2-计数排序算法思想:算法图解:示例代码:在这里插入代码片复杂度分析:3-桶排序算法思想:算法图解:示例代码:在这里插入代码片复杂度分析:4-基数排序算法思想:算法图解:示例代码:在这里插入代码片复杂度分析:…

    2022年7月12日
    18

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号