首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >java stopwatch_java stopwatch 功能

java stopwatch_java stopwatch 功能

作者头像
全栈程序员站长
发布于 2022-09-09 13:20:54
发布于 2022-09-09 13:20:54
44800
代码可运行
举报
运行总次数:0
代码可运行

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

1 /*

2 * Copyright (C) 2008 The Guava Authors3 *4 * Licensed under the Apache License, Version 2.0 (the “License”);5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 *http://www.apache.org/licenses/LICENSE-2.0

9 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an “AS IS” BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */

16

17 packagecom.google.common.base;18

19 import staticcom.google.common.base.Preconditions.checkNotNull;20 import staticcom.google.common.base.Preconditions.checkState;21 import staticjava.util.concurrent.TimeUnit.MICROSECONDS;22 import staticjava.util.concurrent.TimeUnit.MILLISECONDS;23 import staticjava.util.concurrent.TimeUnit.NANOSECONDS;24 import staticjava.util.concurrent.TimeUnit.SECONDS;25

26 importcom.google.common.annotations.Beta;27 importcom.google.common.annotations.GwtCompatible;28 importcom.google.common.annotations.GwtIncompatible;29

30 importjava.util.concurrent.TimeUnit;31

32 /**

33 * An object that measures elapsed time in nanoseconds. It is useful to measure34 * elapsed time using this class instead of direct calls to {@link

35 * System#nanoTime} for a few reasons:36 *37 *

  • 38 *
  • An alternate time source can be substituted, for testing or performance39 * reasons.40 *
  • As documented by {@codenanoTime}, the value returned has no absolute41 * meaning, and can only be interpreted as relative to another timestamp42 * returned by {@codenanoTime} at a different time. {@codeStopwatch} is a43 * more effective abstraction because it exposes only these relative values,44 * not the absolute ones.45 *

46 *47 *

Basic usage:48 *

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
49 *   Stopwatch stopwatch = new Stopwatch().{@link#start start}();50 *   doSomething();51 *   stopwatch.{@link#stop stop}(); // optional52 *53 *   long millis = stopwatch.elapsed(MILLISECONDS);54 *55 *   log.info("that took: " + stopwatch); // formatted string like "12.3 ms"56 * 

57 *58 *

Stopwatch methods are not idempotent; it is an error to start or stop a59 * stopwatch that is already in the desired state.60 *61 *

When testing code that uses this class, use the {@linkplain

62 * #Stopwatch(Ticker) alternate constructor} to supply a fake or mock ticker.63 * This allows you to64 * simulate any valid behavior of the stopwatch.65 *66 *

Note: This class is not thread-safe.67 *68 *@authorKevin Bourrillion69 *@since10.070 */

71 @Beta72 @GwtCompatible(emulated = true)73 public final classStopwatch {74 private finalTicker ticker;75 private booleanisRunning;76 private longelapsedNanos;77 private longstartTick;78

79 /**

80 * Creates (but does not start) a new stopwatch using {@linkSystem#nanoTime}81 * as its time source.82 */

83 publicStopwatch() {84 this(Ticker.systemTicker());85 }86

87 /**

88 * Creates (but does not start) a new stopwatch, using the specified time89 * source.90 */

91 publicStopwatch(Ticker ticker) {92 this.ticker = checkNotNull(ticker, “ticker”);93 }94

95 /**

96 * Returns {@codetrue} if {@link#start()} has been called on this stopwatch,97 * and {@link#stop()} has not been called since the last call to {@code

98 * start()}.99 */

100 public booleanisRunning() {101 returnisRunning;102 }103

104 /**

105 * Starts the stopwatch.106 *107 *@returnthis {@codeStopwatch} instance108 *@throwsIllegalStateException if the stopwatch is already running.109 */

110 publicStopwatch start() {111 checkState(!isRunning,112 “This stopwatch is already running; it cannot be started more than once.”);113 isRunning = true;114 startTick =ticker.read();115 return this;116 }117

118 /**

119 * Stops the stopwatch. Future reads will return the fixed duration that had120 * elapsed up to this point.121 *122 *@returnthis {@codeStopwatch} instance123 *@throwsIllegalStateException if the stopwatch is already stopped.124 */

125 publicStopwatch stop() {126 long tick =ticker.read();127 checkState(isRunning,128 “This stopwatch is already stopped; it cannot be stopped more than once.”);129 isRunning = false;130 elapsedNanos += tick -startTick;131 return this;132 }133

134 /**

135 * Sets the elapsed time for this stopwatch to zero,136 * and places it in a stopped state.137 *138 *@returnthis {@codeStopwatch} instance139 */

140 publicStopwatch reset() {141 elapsedNanos = 0;142 isRunning = false;143 return this;144 }145

146 private longelapsedNanos() {147 return isRunning ? ticker.read() – startTick +elapsedNanos : elapsedNanos;148 }149

150 /**

151 * Returns the current elapsed time shown on this stopwatch, expressed152 * in the desired time unit, with any fraction rounded down.153 *154 *

Note that the overhead of measurement can be more than a microsecond, so155 * it is generally not useful to specify {@linkTimeUnit#NANOSECONDS}156 * precision here.157 *158 *@since14.0 (since 10.0 as {@codeelapsedTime()})159 */

160 public longelapsed(TimeUnit desiredUnit) {161 returndesiredUnit.convert(elapsedNanos(), NANOSECONDS);162 }163

164 /**

165 * Returns the current elapsed time shown on this stopwatch, expressed166 * in the desired time unit, with any fraction rounded down.167 *168 *

Note that the overhead of measurement can be more than a microsecond, so169 * it is generally not useful to specify {@linkTimeUnit#NANOSECONDS}170 * precision here.171 *172 *@deprecatedUse {@linkStopwatch#elapsed(TimeUnit)} instead. This method is173 * scheduled to be removed in Guava release 16.0.174 */

175 @Deprecated176 public longelapsedTime(TimeUnit desiredUnit) {177 returnelapsed(desiredUnit);178 }179

180 /**

181 * Returns the current elapsed time shown on this stopwatch, expressed182 * in milliseconds, with any fraction rounded down. This is identical to183 * {@codeelapsed(TimeUnit.MILLISECONDS)}.184 *185 *@deprecatedUse {@codestopwatch.elapsed(MILLISECONDS)} instead. This186 * method is scheduled to be removed in Guava release 16.0.187 */

188 @Deprecated189 public longelapsedMillis() {190 returnelapsed(MILLISECONDS);191 }192

193 /**

194 * Returns a string representation of the current elapsed time.195 */

196 @GwtIncompatible(“String.format()”)197 @Override publicString toString() {198 return toString(4);199 }200

201 /**

202 * Returns a string representation of the current elapsed time, choosing an203 * appropriate unit and using the specified number of significant figures.204 * For example, at the instant when {@codeelapsed(NANOSECONDS)} would205 * return {1234567}, {@codetoString(4)} returns {@code”1.235 ms”}.206 *207 *@deprecatedUse {@link#toString()} instead. This method is scheduled208 * to be removed in Guava release 15.0.209 */

210 @Deprecated211 @GwtIncompatible(“String.format()”)212 public String toString(intsignificantDigits) {213 long nanos =elapsedNanos();214

215 TimeUnit unit =chooseUnit(nanos);216 double value = (double) nanos / NANOSECONDS.convert(1, unit);217

218 //Too bad this functionality is not exposed as a regular method call

219 return String.format(“%.” + significantDigits + “g %s”,220 value, abbreviate(unit));221 }222

223 private static TimeUnit chooseUnit(longnanos) {224 if (SECONDS.convert(nanos, NANOSECONDS) > 0) {225 returnSECONDS;226 }227 if (MILLISECONDS.convert(nanos, NANOSECONDS) > 0) {228 returnMILLISECONDS;229 }230 if (MICROSECONDS.convert(nanos, NANOSECONDS) > 0) {231 returnMICROSECONDS;232 }233 returnNANOSECONDS;234 }235

236 private staticString abbreviate(TimeUnit unit) {237 switch(unit) {238 caseNANOSECONDS:239 return “ns”;240 caseMICROSECONDS:241 return “\u03bcs”; //渭s

242 caseMILLISECONDS:243 return “ms”;244 caseSECONDS:245 return “s”;246 default:247 throw newAssertionError();248 }249 }250 }

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
H3C配置常用命令
超级用户终端登陆交换机 SYS进入                              //sys进入系统视图 输入以下命令 #  sysname lyz_xmc_bgl                    //sysname 系统名称 #  super password level 3 cipher pass          //superpassword 管理员密码,level3权限为3级,cipher输入密码为隐藏,缺省状态为可见,pass密码字符
py3study
2020/01/07
1.7K0
电信联通负载均衡,NQA联动,实现链路故障自动切换
之前有客户要求,把电信链路配置为上网主要链路,联通链路则作为备份使用,我虽然觉得很浪费,但还是照做了,因为客户总会有自己的考虑。
IT狂人日志
2022/05/18
7720
电信联通负载均衡,NQA联动,实现链路故障自动切换
Linux 集群总结 + LVS(负载均衡器)原理及配置
相信你已经对集群分类有了大致了解了,那么我们现在详细说说使用LVS来实现负载均衡集群。
小土豆Yuki
2020/06/15
3.2K0
Linux 集群总结 + LVS(负载均衡器)原理及配置
网络安全实验07 部署防火墙负载分担双击热备
该为某企业组网拓扑,两台防火墙都工作在网络互连层,上行链接路由器,下行连接二层交换机。防火墙与路由器间运行OSPF协议。现在希望两台防火墙以负载分担模式工作。正常情况下,防火墙A和B共同转发流量。当其中一台防火墙出现故障时,另一台防火墙转发全部业务。
90后小陈老师
2024/04/19
8470
网络安全实验07 部署防火墙负载分担双击热备
CISCO思科路由器配置命令详解及实例
路由器处于用户命令状态,这时用户可以看路由器的连接状态,访问其它网络和主机,但不能看到和更改路由器的设置内容。
网络技术联盟站
2021/04/23
6.9K0
基于LVS-NAT模型的负载均衡调度
首先,需要确定的一点是,LVS-NAT模型中,所有的网络流量都需要流经DS,即包括请求报文和回应报文。当Client端从浏览器或其他客户端请求http或其它网络服务时,先由我们的DS服务器公网网卡接收,然后通过LVS调度挑选一个RS服务器,并通过内部转发机制从其内网网口转发给选中的RS服务器,最后将RS返回的响应报文通过相同的路线反向转回Client端。
用户1456517
2019/03/05
7660
基于LVS-NAT模型的负载均衡调度
网络工程师必知:35个思科设备show命令,排障利器!
在网络工程领域,熟练掌握各种命令是网络工程师必备的技能之一。而在思科设备的管理和维护过程中,show命令是排障、配置和监视网络状态的重要工具。本文将深入介绍35个常用的思科设备show命令,涵盖了配置管理、网络状态查看、邻居和协议状态、安全和监控、高可用性和性能优化、以及VLAN和交换机配置等方面。
网络技术联盟站
2025/01/14
1.3K0
网络工程师必知:35个思科设备show命令,排障利器!
h3c路由器配置命令大全_h3c命令手册
6、 port link-type Access|Trunk|Hybrid 设置端口访问模式
全栈程序员站长
2022/11/02
5.1K0
中小型网络思路规划配置分享,H3C HCL模拟器
采用三层网络结构,核心、汇聚三层互联,堆叠采用40G网络,汇聚10G,接入1G,网关下放到汇聚,交换机采用独立管理VLAN,模拟某工厂真实网络情况。
王忘杰
2022/09/22
8690
中小型网络思路规划配置分享,H3C HCL模拟器
二层交换机与防火墙对接上网配置示例
二层交换机指的是仅能够进行二层转发,不能进行三层转发的交换机。也就是说仅支持二层特性,不支持路由等三层特性的交换机。
知孤云出岫
2023/12/22
6210
二层交换机与防火墙对接上网配置示例
linux route命令的使用详解
route命令用于显示和操作IP路由表。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是 为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为 Linux机器的默认路由。要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;要想永久保存,有如下方法:
墨文
2020/02/28
4.4K0
2022年山东省职业院校技能大赛高职组“网络系统管理”赛项
2.所有交换机和无线控制器开启SSH服务,用户名密码分别为admin、admin1234。密码为明文类型,特权密码为admin。
青灯古酒
2023/10/16
5310
2022年山东省职业院校技能大赛高职组“网络系统管理”赛项
H3C AC+FIT完全设置
       用户采用PON线路,动态分配地址,无固定IP,每月1088元,如果带有固定IP,则需要每月7088元,采用较经济的方式,每次用户查询ip138得到公网IP后远程管理。
py3study
2020/01/08
1.6K0
H3C的命令
# ifconfig eth0 <ip address> netmask <netmask> ;设置IP地址
py3study
2020/01/06
6080
思科模拟器常用命令
计算机命令: PCA login: root                                  :使用root用户 password: linux                                  :口令是linux shutdown -h now                               :同init 0 关机 logout login ifconfig                                        :显示IP地址
阿七日记
2021/12/28
2.8K0
Linux下路由配置梳理
在日常运维作业中,经常会碰到路由表的操作。下面就linux运维中的路由操作做一梳理: ------------------------------------------------------------------------------ 先说一些关于路由的基础知识: 1)路由概念 路由:   跨越从源主机到目标主机的一个互联网络来转发数据包的过程 路由器:能够将数据包转发到正确的目的地,并在转发过程中选择最佳路径的设备 路由表:在路由器中维护的路由条目,路由器根据路由表做路径选择 直连路由:当在路由器
洗尽了浮华
2018/01/23
7.5K0
Linux下路由配置梳理
HCIA数通RS综合实验,附详细配置命令
华三HCL全版本、华为ENSP、Wireshark、VirtualBox全版本、SecureCRT下载!
网络技术联盟站
2023/03/13
1K0
HCIA数通RS综合实验,附详细配置命令
2023年华三H3C HCL新版模拟器防火墙、AC、AP、Phone新功能使用
当前H3C最新版模拟器加入了防火墙、AC、AP、Phone等新设备,本文重点介绍新设备的使用
王忘杰
2023/08/21
9470
2023年华三H3C HCL新版模拟器防火墙、AC、AP、Phone新功能使用
二层交换机与防火墙对接上网配置示例
二层交换机指的是仅能够进行二层转发,不能进行三层转发的交换机。也就是说仅支持二层特性,不支持路由等三层特性的交换机。
知孤云出岫
2024/04/22
2660
二层交换机与防火墙对接上网配置示例
烽火2640路由器命令行手册-04-网络协议配置命令
配置静态ARP映射,静态ARP映射会永久保留在ARP缓存中。如果要删除配置的静态ARP映射的话,使用 no arp 命令。
landv
2018/12/17
1.4K0
推荐阅读
相关推荐
H3C配置常用命令
更多 >
交个朋友
加入数据技术工作实战群
获取实战干货 交流技术经验
加入上海开发者交友群
参与魔都技术局活动 获取合作资源机会
加入程序员求职经验交流群
大厂求职技巧分享 面试经验互助交流
换一批
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验