From 2ead289adeecc1a9f866fc2b22c01cba47071179 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sat, 15 Jul 2023 11:34:55 -0500 Subject: [PATCH] Make ThreadUtil return ThreadPoolExecutor's instead of ExecutorService's This was what was already being done by the Executors.newFixedThreadPool() method we were using; but this change means we don't have to cast the executors if we need additional functionality and we don't have to worry about Java changing Executors.newFixedThreadPool()'s implementation. --- .../distanthorizons/core/util/ThreadUtil.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/util/ThreadUtil.java b/core/src/main/java/com/seibel/distanthorizons/core/util/ThreadUtil.java index a4e146f0f..9e17a82d0 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/util/ThreadUtil.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/util/ThreadUtil.java @@ -2,8 +2,7 @@ package com.seibel.distanthorizons.core.util; import com.seibel.distanthorizons.core.util.objects.DhThreadFactory; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.*; public class ThreadUtil { @@ -16,20 +15,26 @@ public class ThreadUtil // create thread pool // - public static ExecutorService makeThreadPool(int poolSize, String name, int relativePriority) + public static ThreadPoolExecutor makeThreadPool(int poolSize, String name, int relativePriority) { - return Executors.newFixedThreadPool(poolSize, new DhThreadFactory("DH-" + name, Thread.NORM_PRIORITY+relativePriority)); + // this is what was being internally used by Executors.newFixedThreadPool + // I'm just calling it explicitly here so we can reference the more feature-rich + // ThreadPoolExecutor vs the more generic ExecutorService + return new ThreadPoolExecutor(/*corePoolSize*/poolSize, /*maxPoolSize*/poolSize, + 0L, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue(), + new DhThreadFactory("DH-" + name, Thread.NORM_PRIORITY+relativePriority)); } - public static ExecutorService makeThreadPool(int poolSize, Class clazz, int relativePriority) + public static ThreadPoolExecutor makeThreadPool(int poolSize, Class clazz, int relativePriority) { return makeThreadPool(poolSize, clazz.getSimpleName(), relativePriority); } - public static ExecutorService makeThreadPool(int poolSize, String name) + public static ThreadPoolExecutor makeThreadPool(int poolSize, String name) { return makeThreadPool(poolSize, name, 0); } - public static ExecutorService makeThreadPool(int poolSize, Class clazz) + public static ThreadPoolExecutor makeThreadPool(int poolSize, Class clazz) { return makeThreadPool(poolSize, clazz.getSimpleName(), 0); } @@ -37,19 +42,19 @@ public class ThreadUtil // create single thread pool // - public static ExecutorService makeSingleThreadPool(String name, int relativePriority) + public static ThreadPoolExecutor makeSingleThreadPool(String name, int relativePriority) { return makeThreadPool(1, name, relativePriority); } - public static ExecutorService makeSingleThreadPool(Class clazz, int relativePriority) + public static ThreadPoolExecutor makeSingleThreadPool(Class clazz, int relativePriority) { return makeThreadPool(1, clazz.getSimpleName(), relativePriority); } - public static ExecutorService makeSingleThreadPool(String name) + public static ThreadPoolExecutor makeSingleThreadPool(String name) { return makeThreadPool(1, name, 0); } - public static ExecutorService makeSingleThreadPool(Class clazz) + public static ThreadPoolExecutor makeSingleThreadPool(Class clazz) { return makeThreadPool(1, clazz.getSimpleName(), 0); }