我正在使用时间作为我正在调试的程序中的变量。 当我调试这个时,经过时间的变量仍在增加,而我正在检查变量会产生不良结果并使调试失效。 无论如何要冻结时间或阻止变量改变? (我使用System.currentTimeMillis()来计算时间)。
编辑:我有一个空间中的对象随着时间的推移而移动,因为我使用System.currentTimeMillis()来计算已经过了多少时间,它在调试中不起作用。 我想知道我是否有办法解决这个问题,谢谢。
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).
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. I'd like to know if there is a way I can work around this, thanks.
最满意答案
解决此类问题的一种常见方法是使用依赖注入。 创建一个使用时间源初始化(注入)的类Clock(例如)。 在生产代码中,此源可能是System.nanotime()或System.currentTimeMillis()。 在调试中,您使用可控时钟。 Clock类的调试实现有其他方法(例如suspend(),resume(),setClockTo()),允许使用模拟时间。
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. In production code this source might well be System.nanotime() or System.currentTimeMillis(). In debugging you use a controllable clock. The debugging implementation of the Clock class has additional methods (e.g. suspend(), resume(), setClockTo()) that allow one to use simulated time.
调试时间过去了吗?(Time elapsed increases in debugging?)我正在使用时间作为我正在调试的程序中的变量。 当我调试这个时,经过时间的变量仍在增加,而我正在检查变量会产生不良结果并使调试失效。 无论如何要冻结时间或阻止变量改变? (我使用System.currentTimeMillis()来计算时间)。
编辑:我有一个空间中的对象随着时间的推移而移动,因为我使用System.currentTimeMillis()来计算已经过了多少时间,它在调试中不起作用。 我想知道我是否有办法解决这个问题,谢谢。
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).
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. I'd like to know if there is a way I can work around this, thanks.
最满意答案
解决此类问题的一种常见方法是使用依赖注入。 创建一个使用时间源初始化(注入)的类Clock(例如)。 在生产代码中,此源可能是System.nanotime()或System.currentTimeMillis()。 在调试中,您使用可控时钟。 Clock类的调试实现有其他方法(例如suspend(),resume(),setClockTo()),允许使用模拟时间。
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. In production code this source might well be System.nanotime() or System.currentTimeMillis(). In debugging you use a controllable clock. The debugging implementation of the Clock class has additional methods (e.g. suspend(), resume(), setClockTo()) that allow one to use simulated time.
发布评论