This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.commons.threads-3.2.4 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-threads.git
commit db21f6fba23a3f9637ba4f1ac70711d59018b618 Author: Julian Sedding <[email protected]> AuthorDate: Tue Dec 1 13:14:37 2015 +0000 SLING-5343 - Meaningful thread names git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/threads@1717424 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/threads/impl/DefaultThreadPool.java | 7 ++- .../threads/impl/ExtendedThreadFactory.java | 64 ++++++++++++++++------ .../threads/impl/ExtendedThreadFactoryTest.java | 47 ++++++++++++++++ 3 files changed, 100 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java b/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java index 4f5a1a2..e822bb8 100644 --- a/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java +++ b/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java @@ -91,7 +91,12 @@ public class DefaultThreadPool } // Set priority and daemon flag - final ExtendedThreadFactory threadFactory = new ExtendedThreadFactory(delegateThreadFactory, this.configuration.getPriority(), this.configuration.isDaemon()); + final ExtendedThreadFactory threadFactory = new ExtendedThreadFactory( + delegateThreadFactory, + this.name, + this.configuration.getPriority(), + this.configuration.isDaemon() + ); // Keep alive time if (this.configuration.getKeepAliveTime() < 0) { diff --git a/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java b/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java index 87bb6a1..c587d20 100644 --- a/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java +++ b/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java @@ -17,6 +17,7 @@ package org.apache.sling.commons.threads.impl; import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.sling.commons.threads.ThreadPoolConfig; @@ -28,40 +29,55 @@ import org.apache.sling.commons.threads.ThreadPoolConfig; */ public final class ExtendedThreadFactory implements ThreadFactory { - /** The daemon mode */ - private final boolean isDaemon; + /** Template for thread names, for use with String#format() */ + private static final String THREAD_NAME_TEMPLATE = "Sling - %s #%d"; + + /** The real factory. */ + private final ThreadFactory factory; + + /** The name of the thread pool */ + private final String name; /** The priority of newly created Threads */ private final int priority; - /** The real factory. */ - private final ThreadFactory factory; + /** The daemon mode */ + private final boolean isDaemon; + + /** Thread counter for use in thread name */ + private final AtomicInteger threadCounter; /** * Create a new wrapper for a thread factory handling the * + * @param name The name of the thread pool. * @param priority A non null value. * @param isDaemon Whether new {@link Thread}s should run as daemons. */ public ExtendedThreadFactory(final ThreadFactory factory, - final ThreadPoolConfig.ThreadPriority priority, - final boolean isDaemon) { + final String name, + final ThreadPoolConfig.ThreadPriority priority, + final boolean isDaemon) { + this.factory = factory; + this.name = stripPrefixes(name, "Apache Sling ", "Sling "); + this.priority = convertPriority(priority); this.isDaemon = isDaemon; - if ( priority == null ) { + this.threadCounter = new AtomicInteger(1); + } + + private int convertPriority(final ThreadPoolConfig.ThreadPriority priority) { + if (priority == null) { throw new IllegalStateException("Prioriy must not be null."); } - switch ( priority ) { - case NORM : this.priority = Thread.NORM_PRIORITY; - break; - case MIN : this.priority = Thread.MIN_PRIORITY; - break; - case MAX : this.priority = Thread.MAX_PRIORITY; - break; + switch (priority) { + case MIN : + return Thread.MIN_PRIORITY; + case MAX : + return Thread.MAX_PRIORITY; + case NORM : default: // this can never happen - this.priority = Thread.NORM_PRIORITY; - break; + return Thread.NORM_PRIORITY; } - this.factory = factory; } /** @@ -70,9 +86,23 @@ public final class ExtendedThreadFactory implements ThreadFactory { */ public Thread newThread( final Runnable command ) { final Thread thread = this.factory.newThread(command); + thread.setName(nextThreadName()); thread.setPriority( this.priority ); thread.setDaemon( this.isDaemon ); return thread; } + + private String nextThreadName() { + return String.format(THREAD_NAME_TEMPLATE, this.name, this.threadCounter.getAndIncrement()); + } + + private static String stripPrefixes(final String name, final String... prefixes) { + for (final String prefix : prefixes) { + if (name.startsWith(prefix)) { + return name.substring(prefix.length()); + } + } + return name; + } } diff --git a/src/test/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactoryTest.java b/src/test/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactoryTest.java new file mode 100644 index 0000000..ecdc03b --- /dev/null +++ b/src/test/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactoryTest.java @@ -0,0 +1,47 @@ +package org.apache.sling.commons.threads.impl; + +import org.apache.sling.commons.threads.ThreadPoolConfig; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.Executors; + +import static org.junit.Assert.assertEquals; + +public class ExtendedThreadFactoryTest { + + private static final Logger LOG = LoggerFactory.getLogger(ExtendedThreadFactoryTest.class); + + @Test + public void informativeThreadNames() { + final ExtendedThreadFactory tf = createExtendedThreadFactory("Test Pool"); + assertEquals("Thread name", "Sling - Test Pool #1", tf.newThread(null).getName()); + assertEquals("Thread name", "Sling - Test Pool #2", tf.newThread(null).getName()); + } + + @Test + public void shouldStripSlingPrefixFromThreadNames() { + final Thread thread = getFirstThreadFromNamedPool("Sling Test Pool"); + assertEquals("Thread name", "Sling - Test Pool #1", thread.getName()); + } + + @Test + public void shouldStripApacheSlingPrefixFromThreadNames() { + final Thread thread = getFirstThreadFromNamedPool("Apache Sling Test Pool"); + assertEquals("Thread name", "Sling - Test Pool #1", thread.getName()); + } + + private Thread getFirstThreadFromNamedPool(final String poolName) { + return createExtendedThreadFactory(poolName).newThread(null); + } + + private ExtendedThreadFactory createExtendedThreadFactory(final String poolName) { + return new ExtendedThreadFactory( + Executors.defaultThreadFactory(), + poolName, + ThreadPoolConfig.ThreadPriority.NORM, + false + ); + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
