From 6a8fb05156da73dfd26ea645bd0f20b5d06dd401 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Fri, 27 Jan 2023 15:15:24 +0100 Subject: [PATCH] Centralize all ExecutorService creations in Utilities --- .../src/main/java/bisq/common/util/Utilities.java | 14 ++++++++++++++ .../java/bisq/core/app/BisqHeadlessAppMain.java | 13 ++++--------- .../core/app/misc/ExecutableForAppWithP2p.java | 13 ++++--------- .../dao/state/storage/DaoStateStorageService.java | 4 ++-- .../main/java/bisq/daemon/app/BisqDaemonMain.java | 13 ++++--------- .../main/java/bisq/network/Socks5DnsDiscovery.java | 5 ++--- .../java/bisq/network/p2p/network/Connection.java | 3 +-- .../p2p/network/SynchronizedProtoOutputStream.java | 4 ++-- 8 files changed, 33 insertions(+), 36 deletions(-) diff --git a/common/src/main/java/bisq/common/util/Utilities.java b/common/src/main/java/bisq/common/util/Utilities.java index 8343f0326c..1c47d144b7 100644 --- a/common/src/main/java/bisq/common/util/Utilities.java +++ b/common/src/main/java/bisq/common/util/Utilities.java @@ -84,6 +84,11 @@ import static com.google.common.base.Preconditions.checkNotNull; @Slf4j public class Utilities { + public static ExecutorService getSingleThreadExecutor(Class aClass) { + String name = aClass.getSimpleName(); + return getSingleThreadExecutor(name); + } + public static ExecutorService getSingleThreadExecutor(String name) { final ThreadFactory threadFactory = new ThreadFactoryBuilder() .setNameFormat(name) @@ -92,6 +97,15 @@ public class Utilities { return Executors.newSingleThreadExecutor(threadFactory); } + public static ExecutorService getSingleThreadExecutor(ThreadFactory threadFactory) { + return Executors.newSingleThreadExecutor(threadFactory); + } + + public static ExecutorService getFixedThreadPoolExecutor(int nThreads, + ThreadFactory threadFactory) { + return Executors.newFixedThreadPool(nThreads, threadFactory); + } + public static ListeningExecutorService getSingleThreadListeningExecutor(String name) { return MoreExecutors.listeningDecorator(getSingleThreadExecutor(name)); } diff --git a/core/src/main/java/bisq/core/app/BisqHeadlessAppMain.java b/core/src/main/java/bisq/core/app/BisqHeadlessAppMain.java index 4a1de61832..c5923b0528 100644 --- a/core/src/main/java/bisq/core/app/BisqHeadlessAppMain.java +++ b/core/src/main/java/bisq/core/app/BisqHeadlessAppMain.java @@ -22,11 +22,9 @@ import bisq.core.payment.TradeLimits; import bisq.common.UserThread; import bisq.common.app.AppModule; import bisq.common.app.Version; +import bisq.common.util.Utilities; -import com.google.common.util.concurrent.ThreadFactoryBuilder; - -import java.util.concurrent.Executors; -import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ExecutorService; import lombok.extern.slf4j.Slf4j; @@ -62,11 +60,8 @@ public class BisqHeadlessAppMain extends BisqExecutable { @Override protected void configUserThread() { - final ThreadFactory threadFactory = new ThreadFactoryBuilder() - .setNameFormat(this.getClass().getSimpleName()) - .setDaemon(true) - .build(); - UserThread.setExecutor(Executors.newSingleThreadExecutor(threadFactory)); + ExecutorService executorService = Utilities.getSingleThreadExecutor(this.getClass()); + UserThread.setExecutor(executorService); } @Override diff --git a/core/src/main/java/bisq/core/app/misc/ExecutableForAppWithP2p.java b/core/src/main/java/bisq/core/app/misc/ExecutableForAppWithP2p.java index 2c2992f690..7d72c197ae 100644 --- a/core/src/main/java/bisq/core/app/misc/ExecutableForAppWithP2p.java +++ b/core/src/main/java/bisq/core/app/misc/ExecutableForAppWithP2p.java @@ -40,8 +40,7 @@ import bisq.common.handlers.ResultHandler; import bisq.common.persistence.PersistenceManager; import bisq.common.setup.GracefulShutDownHandler; import bisq.common.util.Profiler; - -import com.google.common.util.concurrent.ThreadFactoryBuilder; +import bisq.common.util.Utilities; import java.time.Instant; import java.time.ZoneId; @@ -50,8 +49,7 @@ import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.concurrent.Executors; -import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; @@ -71,11 +69,8 @@ public abstract class ExecutableForAppWithP2p extends BisqExecutable { @Override protected void configUserThread() { - final ThreadFactory threadFactory = new ThreadFactoryBuilder() - .setNameFormat(this.getClass().getSimpleName()) - .setDaemon(true) - .build(); - UserThread.setExecutor(Executors.newSingleThreadExecutor(threadFactory)); + ExecutorService executorService = Utilities.getSingleThreadExecutor(this.getClass()); + UserThread.setExecutor(executorService); } @Override diff --git a/core/src/main/java/bisq/core/dao/state/storage/DaoStateStorageService.java b/core/src/main/java/bisq/core/dao/state/storage/DaoStateStorageService.java index dc44deb90b..c54479fb6a 100644 --- a/core/src/main/java/bisq/core/dao/state/storage/DaoStateStorageService.java +++ b/core/src/main/java/bisq/core/dao/state/storage/DaoStateStorageService.java @@ -29,6 +29,7 @@ import bisq.common.config.Config; import bisq.common.file.FileUtil; import bisq.common.persistence.PersistenceManager; import bisq.common.util.GcUtil; +import bisq.common.util.Utilities; import javax.inject.Inject; import javax.inject.Named; @@ -42,7 +43,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -58,7 +58,7 @@ public class DaoStateStorageService extends StoreService { private final BsqBlocksStorageService bsqBlocksStorageService; private final File storageDir; private final LinkedList blocks = new LinkedList<>(); - private final ExecutorService executorService = Executors.newSingleThreadExecutor(); + private final ExecutorService executorService = Utilities.getSingleThreadExecutor(this.getClass()); private Optional> future = Optional.empty(); diff --git a/daemon/src/main/java/bisq/daemon/app/BisqDaemonMain.java b/daemon/src/main/java/bisq/daemon/app/BisqDaemonMain.java index a9e4ce2aae..7fe4b633a3 100644 --- a/daemon/src/main/java/bisq/daemon/app/BisqDaemonMain.java +++ b/daemon/src/main/java/bisq/daemon/app/BisqDaemonMain.java @@ -24,11 +24,9 @@ import bisq.core.app.CoreModule; import bisq.common.UserThread; import bisq.common.app.AppModule; import bisq.common.handlers.ResultHandler; +import bisq.common.util.Utilities; -import com.google.common.util.concurrent.ThreadFactoryBuilder; - -import java.util.concurrent.Executors; -import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ExecutorService; import lombok.extern.slf4j.Slf4j; @@ -51,11 +49,8 @@ public class BisqDaemonMain extends BisqHeadlessAppMain implements BisqSetup.Bis @Override protected void configUserThread() { - final ThreadFactory threadFactory = new ThreadFactoryBuilder() - .setNameFormat(this.getClass().getSimpleName()) - .setDaemon(true) - .build(); - UserThread.setExecutor(Executors.newSingleThreadExecutor(threadFactory)); + ExecutorService executorService = Utilities.getSingleThreadExecutor(this.getClass()); + UserThread.setExecutor(executorService); } @Override diff --git a/p2p/src/main/java/bisq/network/Socks5DnsDiscovery.java b/p2p/src/main/java/bisq/network/Socks5DnsDiscovery.java index f6ee7cf7e5..ce66da8984 100644 --- a/p2p/src/main/java/bisq/network/Socks5DnsDiscovery.java +++ b/p2p/src/main/java/bisq/network/Socks5DnsDiscovery.java @@ -34,7 +34,6 @@ import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; @@ -87,9 +86,9 @@ public class Socks5DnsDiscovery extends MultiplexingDiscovery { // Attempted workaround for reported bugs on Linux in which gethostbyname does not appear to be properly // thread safe and can cause segfaults on some libc versions. if (Utilities.isLinux()) - return Executors.newSingleThreadExecutor(new ContextPropagatingThreadFactory("DNS seed lookups")); + return Utilities.getSingleThreadExecutor(new ContextPropagatingThreadFactory("DNS seed lookups")); else - return Executors.newFixedThreadPool(seeds.size(), new DaemonThreadFactory("DNS seed lookups")); + return Utilities.getFixedThreadPoolExecutor(seeds.size(), new DaemonThreadFactory("DNS seed lookups")); } /** diff --git a/p2p/src/main/java/bisq/network/p2p/network/Connection.java b/p2p/src/main/java/bisq/network/p2p/network/Connection.java index a96cf7df10..df58d00169 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/Connection.java +++ b/p2p/src/main/java/bisq/network/p2p/network/Connection.java @@ -74,7 +74,6 @@ import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; @@ -129,7 +128,7 @@ public class Connection implements HasCapabilities, Runnable, MessageListener { private final NetworkFilter networkFilter; @Getter private final String uid; - private final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "Connection.java executor-service")); + private final ExecutorService singleThreadExecutor = Utilities.getSingleThreadExecutor(runnable -> new Thread(runnable, "Connection.java executor-service")); @Getter private final Statistic statistic; @Getter diff --git a/p2p/src/main/java/bisq/network/p2p/network/SynchronizedProtoOutputStream.java b/p2p/src/main/java/bisq/network/p2p/network/SynchronizedProtoOutputStream.java index 96d944c75f..f36af36056 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/SynchronizedProtoOutputStream.java +++ b/p2p/src/main/java/bisq/network/p2p/network/SynchronizedProtoOutputStream.java @@ -18,12 +18,12 @@ package bisq.network.p2p.network; import bisq.common.proto.network.NetworkEnvelope; +import bisq.common.util.Utilities; import java.io.OutputStream; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import org.slf4j.Logger; @@ -39,7 +39,7 @@ class SynchronizedProtoOutputStream extends ProtoOutputStream { SynchronizedProtoOutputStream(OutputStream delegate, Statistic statistic) { super(delegate, statistic); - this.executorService = Executors.newSingleThreadExecutor(); + this.executorService = Utilities.getSingleThreadExecutor(this.getClass()); } @Override