diff --git a/jpos/src/main/java/org/jpos/space/JDBMSpace.java b/jpos/src/main/java/org/jpos/space/JDBMSpace.java index 4bdec89f6f..7f3fc13f6c 100644 --- a/jpos/src/main/java/org/jpos/space/JDBMSpace.java +++ b/jpos/src/main/java/org/jpos/space/JDBMSpace.java @@ -322,13 +322,14 @@ public synchronized V in (Object key) { */ public synchronized V in (Object key, long timeout) { Object obj; - Instant now = Instant.now(); - long duration; + Duration to = Duration.ofMillis(timeout); + Duration now = Duration.ofNanos(System.nanoTime()); + Duration duration; while ((obj = inp (key)) == null && - (duration = Duration.between(now, Instant.now()).toMillis()) < timeout) + to.compareTo(duration = Duration.ofNanos(System.nanoTime()).minus(now)) > 0) { try { - this.wait (timeout - duration); + this.wait (Math.max(to.minus(duration).toMillis(), 1L)); } catch (InterruptedException ignored) { } } return (V) obj; @@ -358,13 +359,14 @@ public synchronized V rd (Object key) { */ public synchronized V rd (Object key, long timeout) { Object obj; - Instant now = Instant.now(); - long duration; + Duration to = Duration.ofMillis(timeout); + Duration now = Duration.ofNanos(System.nanoTime()); + Duration duration; while ((obj = rdp (key)) == null && - (duration = Duration.between(now, Instant.now()).toMillis()) < timeout) + to.compareTo(duration = Duration.ofNanos(System.nanoTime()).minus(now)) > 0) { try { - this.wait (timeout - duration); + this.wait (Math.max(to.minus(duration).toMillis(), 1L)); } catch (InterruptedException ignored) { } } return (V) obj; @@ -378,13 +380,14 @@ public synchronized void nrd (Object key) { } public synchronized V nrd (Object key, long timeout) { Object obj; - Instant now = Instant.now(); - long duration; + Duration to = Duration.ofMillis(timeout); + Duration now = Duration.ofNanos(System.nanoTime()); + Duration duration; while ((obj = rdp (key)) != null && - (duration = Duration.between(now, Instant.now()).toMillis()) < timeout) + to.compareTo(duration = Duration.ofNanos(System.nanoTime()).minus(now)) > 0) { try { - this.wait (Math.min(NRD_RESOLUTION, timeout - duration)); + this.wait (Math.min(NRD_RESOLUTION, Math.max(to.minus(duration).toMillis(), 1L))); } catch (InterruptedException ignored) { } } return (V) obj; @@ -410,14 +413,15 @@ public boolean existAny (Object[] keys) { return false; } public boolean existAny (Object[] keys, long timeout) { - Instant now = Instant.now(); - long duration; - while ((duration = Duration.between(now, Instant.now()).toMillis()) < timeout) { + Duration to = Duration.ofMillis(timeout); + Duration now = Duration.ofNanos(System.nanoTime()); + Duration duration; + while (to.compareTo(duration = Duration.ofNanos(System.nanoTime()).minus(now)) > 0) { if (existAny (keys)) return true; synchronized (this) { try { - wait (timeout - duration); + wait (Math.max(to.minus(duration).toMillis(), 1L)); } catch (InterruptedException ignored) { } } } diff --git a/jpos/src/test/java/org/jpos/space/JDBMSpaceTestCase.java b/jpos/src/test/java/org/jpos/space/JDBMSpaceTestCase.java index bcc95b86da..c8a412599a 100644 --- a/jpos/src/test/java/org/jpos/space/JDBMSpaceTestCase.java +++ b/jpos/src/test/java/org/jpos/space/JDBMSpaceTestCase.java @@ -35,7 +35,6 @@ import java.nio.file.Path; import java.time.Duration; -import java.time.Instant; @SuppressWarnings("unchecked") public class JDBMSpaceTestCase { @@ -238,12 +237,12 @@ public void run() { sp.out ("KA", Boolean.TRUE); } }.start(); - Instant now = Instant.now(); + Duration now = Duration.ofNanos(System.nanoTime()); assertTrue ( sp.existAny(new String[]{"KA", "KB"}, 2000L), "existAnyWithTimeout ([KA,KB], delay)" ); - long elapsed = Duration.between(now, Instant.now()).toMillis(); + long elapsed = Duration.ofNanos(System.nanoTime()).minus(now).toMillis(); assertTrue (elapsed > 900L, "delay was > 1000"); assertNotNull (sp.inp("KA"), "Entry should not be null"); }