2222import dev .failsafe .spi .PolicyExecutor ;
2323
2424import java .time .Duration ;
25+ import java .time .Instant ;
2526import java .util .ArrayList ;
2627import java .util .List ;
2728import java .util .ListIterator ;
@@ -39,7 +40,7 @@ class ExecutionImpl<R> implements ExecutionInternal<R> {
3940
4041 final List <PolicyExecutor <R >> policyExecutors ;
4142 // When the first execution attempt was started
42- private volatile Duration startTime ;
43+ private volatile Instant startTime ;
4344 // Number of execution attempts
4445 private final AtomicInteger attempts ;
4546 // Number of completed executions
@@ -54,7 +55,7 @@ class ExecutionImpl<R> implements ExecutionInternal<R> {
5455 // The result of the current execution attempt;
5556 volatile ExecutionResult <R > result ;
5657 // When the most recent execution attempt was started
57- volatile Duration attemptStartTime ;
58+ private volatile Instant attemptStartTime ;
5859 // The index of a PolicyExecutor that cancelled the execution. Integer.MIN_VALUE represents non-cancelled.
5960 volatile int cancelledIndex = Integer .MIN_VALUE ;
6061 // The user-provided callback to be called when an execution is cancelled
@@ -71,8 +72,6 @@ class ExecutionImpl<R> implements ExecutionInternal<R> {
7172 */
7273 ExecutionImpl (List <? extends Policy <R >> policies ) {
7374 policyExecutors = new ArrayList <>(policies .size ());
74- startTime = Duration .ZERO ;
75- attemptStartTime = Duration .ZERO ;
7675 attempts = new AtomicInteger ();
7776 executions = new AtomicInteger ();
7877 latest = new AtomicReference <>(this );
@@ -122,8 +121,8 @@ public void onCancel(CheckedRunnable cancelCallback) {
122121 @ Override
123122 public synchronized void preExecute () {
124123 if (!preExecuted ) {
125- attemptStartTime = Duration . ofNanos ( System . nanoTime () );
126- if (startTime == Duration . ZERO )
124+ attemptStartTime = Instant . now ( );
125+ if (startTime == null )
127126 startTime = attemptStartTime ;
128127 preExecuted = true ;
129128 }
@@ -220,12 +219,12 @@ public ExecutionInternal<R> getLatest() {
220219
221220 @ Override
222221 public Duration getElapsedTime () {
223- return Duration .ofNanos ( System . nanoTime () - startTime . toNanos ());
222+ return startTime == null ? Duration .ZERO : Duration . between ( startTime , Instant . now ());
224223 }
225224
226225 @ Override
227226 public Duration getElapsedAttemptTime () {
228- return Duration .ofNanos ( System . nanoTime () - attemptStartTime . toNanos ());
227+ return attemptStartTime == null ? Duration .ZERO : Duration . between ( attemptStartTime , Instant . now ());
229228 }
230229
231230 @ Override
@@ -264,7 +263,7 @@ public R getLastResult(R defaultValue) {
264263 }
265264
266265 @ Override
267- public Duration getStartTime () {
266+ public Instant getStartTime () {
268267 return startTime ;
269268 }
270269
0 commit comments