From 6c886e4b8a4b8c1b38688dc69dfd8f9627a2dccf Mon Sep 17 00:00:00 2001 From: Nikita Lebedev Date: Mon, 18 Sep 2023 15:54:23 +0300 Subject: [PATCH] Hotfix issue #1606. Signed-off-by: Nikita Lebedev --- .../java/com/hedera/hashgraph/sdk/Delayer.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/sdk/src/main/java/com/hedera/hashgraph/sdk/Delayer.java b/sdk/src/main/java/com/hedera/hashgraph/sdk/Delayer.java index 33b53df59..cb187f221 100644 --- a/sdk/src/main/java/com/hedera/hashgraph/sdk/Delayer.java +++ b/sdk/src/main/java/com/hedera/hashgraph/sdk/Delayer.java @@ -20,6 +20,8 @@ package com.hedera.hashgraph.sdk; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.time.Duration; @@ -33,6 +35,12 @@ final class Delayer { private static final Logger logger = LoggerFactory.getLogger(Delayer.class); + private static final ScheduledExecutorService SCHEDULER = Executors.newSingleThreadScheduledExecutor(r -> { + Thread t = new Thread(r); + t.setDaemon(true); + return t; + }); + private static final Duration MIN_DELAY = Duration.ofMillis(500); /** @@ -67,6 +75,10 @@ static CompletableFuture delayFor(long milliseconds, Executor executor) { return CompletableFuture.runAsync( () -> { }, - CompletableFuture.delayedExecutor(milliseconds, TimeUnit.MILLISECONDS, executor)); + delayedExecutor(milliseconds, TimeUnit.MILLISECONDS, executor)); + } + + private static Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) { + return r -> SCHEDULER.schedule(() -> executor.execute(r), delay, unit); } }