From b69a7c1b4871058d54a283551cc61e7d3c0e3490 Mon Sep 17 00:00:00 2001 From: woodser Date: Thu, 9 Feb 2023 11:23:53 -0500 Subject: [PATCH] use thread pool to cap sync concurrency in setDaemonConnection() --- .../java/bisq/core/trade/HavenoUtils.java | 24 ++++++++++++++++--- core/src/main/java/bisq/core/trade/Trade.java | 4 ++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/bisq/core/trade/HavenoUtils.java b/core/src/main/java/bisq/core/trade/HavenoUtils.java index aaecc59ee9..3fcc997145 100644 --- a/core/src/main/java/bisq/core/trade/HavenoUtils.java +++ b/core/src/main/java/bisq/core/trade/HavenoUtils.java @@ -67,6 +67,10 @@ public class HavenoUtils { public static BigInteger CENTINEROS_AU_MULTIPLIER = new BigInteger("10000"); private static BigInteger XMR_AU_MULTIPLIER = new BigInteger("1000000000000"); + // global thread pool + private static final int POOL_SIZE = 10; + private static final ExecutorService POOL = Executors.newFixedThreadPool(POOL_SIZE); + // TODO: better way to share reference? public static ArbitrationManager arbitrationManager; @@ -349,14 +353,28 @@ public class HavenoUtils { } } - // TODO: replace with GenUtils.executeTasks() + /** + * Submit tasks to a global thread pool. + */ + public static Future submitTask(Runnable task) { + return POOL.submit(task); + } + + public static List> submitTasks(List tasks) { + List> futures = new ArrayList>(); + for (Runnable task : tasks) futures.add(submitTask(task)); + return futures; + } + + // TODO: replace with GenUtils.executeTasks() once monero-java updated + public static void executeTasks(Collection tasks) { executeTasks(tasks, tasks.size()); } - public static void executeTasks(Collection tasks, int poolSize) { + public static void executeTasks(Collection tasks, int maxConcurrency) { if (tasks.isEmpty()) return; - ExecutorService pool = Executors.newFixedThreadPool(poolSize); + ExecutorService pool = Executors.newFixedThreadPool(maxConcurrency); List> futures = new ArrayList>(); for (Runnable task : tasks) futures.add(pool.submit(task)); pool.shutdown(); diff --git a/core/src/main/java/bisq/core/trade/Trade.java b/core/src/main/java/bisq/core/trade/Trade.java index b398ef96e1..877eb64252 100644 --- a/core/src/main/java/bisq/core/trade/Trade.java +++ b/core/src/main/java/bisq/core/trade/Trade.java @@ -1590,13 +1590,13 @@ public abstract class Trade implements Tradable, Model { wallet.setDaemonConnection(connection); // sync and reprocess messages on new thread - new Thread(() -> { + HavenoUtils.submitTask(() -> { updateSyncing(); // reprocess pending payout messages this.getProtocol().maybeReprocessPaymentReceivedMessage(false); HavenoUtils.arbitrationManager.maybeReprocessDisputeClosedMessage(this, false); - }).start(); + }); } private void updateSyncing() {