Use @Serial in util
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / CachedThreadPoolExecutor.java
index 141908050971cb9ea953d0e4935a2e272268dc3e..e3f1f40f4ce5a0c960285e1a73832d9c199df553 100644 (file)
@@ -11,7 +11,8 @@ import static java.util.Objects.requireNonNull;
 
 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;
@@ -19,6 +20,7 @@ import java.util.concurrent.RejectedExecutionHandler;
 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
@@ -50,8 +52,13 @@ public class CachedThreadPoolExecutor extends ThreadPoolExecutor {
      *            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
@@ -67,8 +74,8 @@ public class CachedThreadPoolExecutor extends ThreadPoolExecutor {
         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();
 
@@ -93,7 +100,7 @@ public class CachedThreadPoolExecutor extends ThreadPoolExecutor {
     }
 
     public long getLargestQueueSize() {
-        return ((TrackingLinkedBlockingQueue<?>)executorQueue.getBackingQueue()).getLargestQueueSize();
+        return executorQueue.getBackingQueue().getLargestQueueSize();
     }
 
     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
@@ -123,18 +130,20 @@ public class CachedThreadPoolExecutor extends ThreadPoolExecutor {
      * 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;
         }