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
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));
}

View File

@ -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

View File

@ -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

View File

@ -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<DaoStateStore> {
private final BsqBlocksStorageService bsqBlocksStorageService;
private final File storageDir;
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();

View File

@ -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

View File

@ -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"));
}
/**

View File

@ -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

View File

@ -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