Centralize all ExecutorService creations in Utilities

This commit is contained in:
Alva Swanson 2023-01-27 15:15:24 +01:00
parent d682d956a1
commit 6a8fb05156
No known key found for this signature in database
GPG Key ID: 004760E77F753090
8 changed files with 33 additions and 36 deletions

View File

@ -84,6 +84,11 @@ import static com.google.common.base.Preconditions.checkNotNull;
@Slf4j @Slf4j
public class Utilities { public class Utilities {
public static ExecutorService getSingleThreadExecutor(Class<?> aClass) {
String name = aClass.getSimpleName();
return getSingleThreadExecutor(name);
}
public static ExecutorService getSingleThreadExecutor(String name) { public static ExecutorService getSingleThreadExecutor(String name) {
final ThreadFactory threadFactory = new ThreadFactoryBuilder() final ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setNameFormat(name) .setNameFormat(name)
@ -92,6 +97,15 @@ public class Utilities {
return Executors.newSingleThreadExecutor(threadFactory); 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) { public static ListeningExecutorService getSingleThreadListeningExecutor(String name) {
return MoreExecutors.listeningDecorator(getSingleThreadExecutor(name)); return MoreExecutors.listeningDecorator(getSingleThreadExecutor(name));
} }

View File

@ -22,11 +22,9 @@ import bisq.core.payment.TradeLimits;
import bisq.common.UserThread; import bisq.common.UserThread;
import bisq.common.app.AppModule; import bisq.common.app.AppModule;
import bisq.common.app.Version; import bisq.common.app.Version;
import bisq.common.util.Utilities;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -62,11 +60,8 @@ public class BisqHeadlessAppMain extends BisqExecutable {
@Override @Override
protected void configUserThread() { protected void configUserThread() {
final ThreadFactory threadFactory = new ThreadFactoryBuilder() ExecutorService executorService = Utilities.getSingleThreadExecutor(this.getClass());
.setNameFormat(this.getClass().getSimpleName()) UserThread.setExecutor(executorService);
.setDaemon(true)
.build();
UserThread.setExecutor(Executors.newSingleThreadExecutor(threadFactory));
} }
@Override @Override

View File

@ -40,8 +40,7 @@ import bisq.common.handlers.ResultHandler;
import bisq.common.persistence.PersistenceManager; import bisq.common.persistence.PersistenceManager;
import bisq.common.setup.GracefulShutDownHandler; import bisq.common.setup.GracefulShutDownHandler;
import bisq.common.util.Profiler; import bisq.common.util.Profiler;
import bisq.common.util.Utilities;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
@ -50,8 +49,7 @@ import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -71,11 +69,8 @@ public abstract class ExecutableForAppWithP2p extends BisqExecutable {
@Override @Override
protected void configUserThread() { protected void configUserThread() {
final ThreadFactory threadFactory = new ThreadFactoryBuilder() ExecutorService executorService = Utilities.getSingleThreadExecutor(this.getClass());
.setNameFormat(this.getClass().getSimpleName()) UserThread.setExecutor(executorService);
.setDaemon(true)
.build();
UserThread.setExecutor(Executors.newSingleThreadExecutor(threadFactory));
} }
@Override @Override

View File

@ -29,6 +29,7 @@ import bisq.common.config.Config;
import bisq.common.file.FileUtil; import bisq.common.file.FileUtil;
import bisq.common.persistence.PersistenceManager; import bisq.common.persistence.PersistenceManager;
import bisq.common.util.GcUtil; import bisq.common.util.GcUtil;
import bisq.common.util.Utilities;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -42,7 +43,6 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -58,7 +58,7 @@ public class DaoStateStorageService extends StoreService<DaoStateStore> {
private final BsqBlocksStorageService bsqBlocksStorageService; private final BsqBlocksStorageService bsqBlocksStorageService;
private final File storageDir; private final File storageDir;
private final LinkedList<Block> blocks = new LinkedList<>(); private final LinkedList<Block> blocks = new LinkedList<>();
private final ExecutorService executorService = Executors.newSingleThreadExecutor(); private final ExecutorService executorService = Utilities.getSingleThreadExecutor(this.getClass());
private Optional<Future<?>> future = Optional.empty(); private Optional<Future<?>> future = Optional.empty();

View File

@ -24,11 +24,9 @@ import bisq.core.app.CoreModule;
import bisq.common.UserThread; import bisq.common.UserThread;
import bisq.common.app.AppModule; import bisq.common.app.AppModule;
import bisq.common.handlers.ResultHandler; import bisq.common.handlers.ResultHandler;
import bisq.common.util.Utilities;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -51,11 +49,8 @@ public class BisqDaemonMain extends BisqHeadlessAppMain implements BisqSetup.Bis
@Override @Override
protected void configUserThread() { protected void configUserThread() {
final ThreadFactory threadFactory = new ThreadFactoryBuilder() ExecutorService executorService = Utilities.getSingleThreadExecutor(this.getClass());
.setNameFormat(this.getClass().getSimpleName()) UserThread.setExecutor(executorService);
.setDaemon(true)
.build();
UserThread.setExecutor(Executors.newSingleThreadExecutor(threadFactory));
} }
@Override @Override

View File

@ -34,7 +34,6 @@ import java.net.InetSocketAddress;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j; 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 // 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. // thread safe and can cause segfaults on some libc versions.
if (Utilities.isLinux()) if (Utilities.isLinux())
return Executors.newSingleThreadExecutor(new ContextPropagatingThreadFactory("DNS seed lookups")); return Utilities.getSingleThreadExecutor(new ContextPropagatingThreadFactory("DNS seed lookups"));
else else
return Executors.newFixedThreadPool(seeds.size(), new DaemonThreadFactory("DNS seed lookups")); return Utilities.getFixedThreadPoolExecutor(seeds.size(), new DaemonThreadFactory("DNS seed lookups"));
} }
/** /**

View File

@ -74,7 +74,6 @@ import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -129,7 +128,7 @@ public class Connection implements HasCapabilities, Runnable, MessageListener {
private final NetworkFilter networkFilter; private final NetworkFilter networkFilter;
@Getter @Getter
private final String uid; 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 @Getter
private final Statistic statistic; private final Statistic statistic;
@Getter @Getter

View File

@ -18,12 +18,12 @@
package bisq.network.p2p.network; package bisq.network.p2p.network;
import bisq.common.proto.network.NetworkEnvelope; import bisq.common.proto.network.NetworkEnvelope;
import bisq.common.util.Utilities;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -39,7 +39,7 @@ class SynchronizedProtoOutputStream extends ProtoOutputStream {
SynchronizedProtoOutputStream(OutputStream delegate, Statistic statistic) { SynchronizedProtoOutputStream(OutputStream delegate, Statistic statistic) {
super(delegate, statistic); super(delegate, statistic);
this.executorService = Executors.newSingleThreadExecutor(); this.executorService = Utilities.getSingleThreadExecutor(this.getClass());
} }
@Override @Override