简体   繁体   English

调试时间过去了吗?

[英]Time elapsed increases in debugging?

I'm using time elapsed as a variable in a program which I am currently debugging. 我正在使用时间作为我正在调试的程序中的变量。 As I am debugging this the time elapsed variable still increases whilst I am examining variables giving undesired results and makes debugging ineffective. 在我调试时,经过时间的变量仍在增加,而我正在检查变量会产生不良结果并使调试失效。 Is there anyway to freeze time or stop the variable from changing? 无论如何要冻结时间或阻止变量改变? (I am using System.currentTimeMillis() to calculate the time). (我正在使用System.currentTimeMillis()来计算时间)。

Edit: I have an object in a space that moves as time increases, because I am using System.currentTimeMillis() to calculate how much time has passed, it doesn't work in debugging. 编辑:我有一个空间中的对象随着时间的推移而移动,因为我使用System.currentTimeMillis()来计算已经过了多少时间,它在调试中不起作用。 I'd like to know if there is a way I can work around this, thanks. 我想知道是否有办法可以解决这个问题,谢谢。

System.currentTimeMillis() does what it is supposed to do: Give you the time at the moment the instruction is executed. System.currentTimeMillis()执行它应该执行的操作:在执行指令时给你时间。 When you are "examining variables" your program thread is suspended but time still is moving on... Thus the time increase 当你“检查变量”时,你的程序线程被暂停,但时间仍在继续...因此时间增加

One common approach to this kind of problem is to use Dependency Injection. 解决此类问题的一种常见方法是使用依赖注入。 Create a class Clock (for example) which is initialized (injected) with a time source. 创建一个使用时间源初始化(注入)的类Clock(例如)。 In production code this source might well be System.nanotime() or System.currentTimeMillis(). 在生产代码中,此源可能是System.nanotime()或System.currentTimeMillis()。 In debugging you use a controllable clock. 在调试中,您使用可控时钟。 The debugging implementation of the Clock class has additional methods (eg suspend(), resume(), setClockTo()) that allow one to use simulated time. Clock类的调试实现有其他方法(例如suspend(),resume(),setClockTo()),允许使用模拟时间。

Your alternative is to not use to System.currentTimeMillis(). 您的替代方法是不使用System.currentTimeMillis()。 This is rather complicated because when you are debugging you want to time to step forward when the code is running. 这相当复杂,因为在调试时,您希望在代码运行时有时间前进。

The best solution is to change this requirement, so that this is not a problem. 最好的解决方案是更改此要求,这样就不会出现问题。

eg say you have a timeout which is short, you can have it increase the timeout when you are running in the debugger. 例如,假设您有一个短暂的超时,您可以在调试器中运行时增加超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM