diff --git a/src/main/java/com/commafeed/backend/feeds/FeedRefreshExecutor.java b/src/main/java/com/commafeed/backend/feeds/FeedRefreshExecutor.java index 232aef98..31f16c3c 100644 --- a/src/main/java/com/commafeed/backend/feeds/FeedRefreshExecutor.java +++ b/src/main/java/com/commafeed/backend/feeds/FeedRefreshExecutor.java @@ -2,8 +2,10 @@ package com.commafeed.backend.feeds; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,6 +21,7 @@ public class FeedRefreshExecutor { public FeedRefreshExecutor(final String poolName, int threads, int queueCapacity) { + log.info("Creating pool {} with {} threads", poolName, threads); this.poolName = poolName; pool = new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS, @@ -52,6 +55,7 @@ public class FeedRefreshExecutor { } } }); + pool.setThreadFactory(new NamedThreadFactory(poolName)); } public void execute(Task task) { @@ -82,4 +86,28 @@ public class FeedRefreshExecutor { } } } + + private static class NamedThreadFactory implements ThreadFactory { + private final ThreadGroup group; + private final AtomicInteger threadNumber = new AtomicInteger(1); + private final String namePrefix; + + private NamedThreadFactory(String poolName) { + SecurityManager s = System.getSecurityManager(); + group = (s != null) ? s.getThreadGroup() : + Thread.currentThread().getThreadGroup(); + namePrefix = poolName + "-thread-"; + } + + public Thread newThread(Runnable r) { + Thread t = new Thread(group, r, + namePrefix + threadNumber.getAndIncrement(), + 0); + if (t.isDaemon()) + t.setDaemon(false); + if (t.getPriority() != Thread.NORM_PRIORITY) + t.setPriority(Thread.NORM_PRIORITY); + return t; + } + } }