Add newCachedThreadPool to Utilities

Use newCachedThreadPool instead of getThreadPoolExecutor

Utilities.getThreadPoolExecutor use a BlockingQueue which prevents the intended
behaviour to increase the pool size to the max value.

Signed-off-by: HenrikJannsen <boilingfrog@gmx.com>
This commit is contained in:
HenrikJannsen 2022-12-08 09:56:11 -05:00
parent f4775f89b0
commit 3308e35e8c
No known key found for this signature in database
GPG key ID: 02AA2BAE387C8307
2 changed files with 10 additions and 3 deletions

View file

@ -64,6 +64,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@ -135,6 +136,12 @@ public class Utilities {
return executor;
}
public static ExecutorService newCachedThreadPool(int maximumPoolSize) {
return new ThreadPoolExecutor(0, maximumPoolSize,
60, TimeUnit.SECONDS,
new SynchronousQueue<>());
}
@SuppressWarnings("SameParameterValue")
public static ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor(String name,
int corePoolSize,

View file

@ -60,7 +60,7 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
@ -98,7 +98,7 @@ public class SeedNodeReportingService {
private Timer dataReportTimer;
private final Timer heartBeatTimer;
private final ThreadPoolExecutor executor;
private final ExecutorService executor;
@Inject
public SeedNodeReportingService(P2PService p2PService,
@ -123,7 +123,7 @@ public class SeedNodeReportingService {
this.maxConnections = maxConnections;
this.seedNodeReportingServerUrl = seedNodeReportingServerUrl;
executor = Utilities.getThreadPoolExecutor("SeedNodeReportingService", 2, 4, 30);
executor = Utilities.newCachedThreadPool(5);
httpClient = HttpClient.newHttpClient();
heartBeatTimer = UserThread.runPeriodically(this::sendHeartBeat, HEART_BEAT_DELAY_SEC);