import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.io.Serial;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
+import org.slf4j.LoggerFactory;
/**
* A ThreadPoolExecutor with a specified bounded queue capacity that favors reusing previously
* the capacity of the queue.
* @param threadPrefix
* the name prefix for threads created by this executor.
+ * @param loggerIdentity
+ * the class to use as logger name for logging uncaught exceptions from the threads.
*/
- public CachedThreadPoolExecutor(final int maximumPoolSize, final int maximumQueueSize, final String threadPrefix) {
+ // due to loggerIdentity argument usage
+ @SuppressWarnings("checkstyle:LoggerFactoryClassParameter")
+ public CachedThreadPoolExecutor(final int maximumPoolSize, final int maximumQueueSize, final String threadPrefix,
+ final Class<?> loggerIdentity) {
// We're using a custom SynchronousQueue that has a backing bounded LinkedBlockingQueue.
// We don't specify any core threads (first parameter) so, when a task is submitted,
// the base class will always try to offer to the queue. If there is an existing waiting
this.threadPrefix = requireNonNull(threadPrefix);
this.maximumQueueSize = maximumQueueSize;
- setThreadFactory(new ThreadFactoryBuilder().setDaemon(true)
- .setNameFormat(this.threadPrefix + "-%d").build());
+ setThreadFactory(ThreadFactoryProvider.builder().namePrefix(threadPrefix)
+ .logger(LoggerFactory.getLogger(loggerIdentity)).build().get());
executorQueue = (ExecutorQueue)super.getQueue();
}
public long getLargestQueueSize() {
- return ((TrackingLinkedBlockingQueue<?>)executorQueue.getBackingQueue()).getLargestQueueSize();
+ return executorQueue.getBackingQueue().getLargestQueueSize();
}
protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
* threads are busy.
*/
private static class ExecutorQueue extends SynchronousQueue<Runnable> {
-
+ @Serial
private static final long serialVersionUID = 1L;
private static final long POLL_WAIT_TIME_IN_MS = 300;
- private final LinkedBlockingQueue<Runnable> backingQueue;
+ @SuppressFBWarnings("SE_BAD_FIELD")
+ // Runnable is not Serializable
+ private final TrackingLinkedBlockingQueue<Runnable> backingQueue;
ExecutorQueue(final int maxBackingQueueSize) {
backingQueue = new TrackingLinkedBlockingQueue<>(maxBackingQueueSize);
}
- LinkedBlockingQueue<Runnable> getBackingQueue() {
+ TrackingLinkedBlockingQueue<Runnable> getBackingQueue() {
return backingQueue;
}