java中时钟跳动_Java中是否存在一致(单调)时钟实现?

默认java.time.Clock实现基于System.currentTimeMillis()。如这里所讨论的,

Monotonically increasing time in Java?,

它不能保证是单调的。

事实上,我经常遇到一个情况,系统时间自动调整到几秒钟,而java时钟也跳回来。

//now() returns 2016-01-13T22:34:05.681Z

order.setCreationTime(Instant.now());

//... something happens, order gets cancelled

//now() returns 2016-01-13T22:34:03.123Z

//which is a few seconds before the former one,

//even though the call was performed later - in any reasonable sense.

//The recorded history of events is obviously inconsistent with the real world.

order.setCancelationTime(Instant.now());

当不能依赖时间只在一个方向上时,不可能执行时间敏感的事情,例如记录和分析事件历史。

上述帖子说System.nanoTime()是单调的(如果底层系统支持它)。所以,如果我想将我的代码放在java.time API上,那我需要在内部使用nanoTime的Clock来确保单向流程。也许这样的事情会起作用。还是不行?

public class MyClock() extends java.time.Clock { </

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值