You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by js...@apache.org on 2015/12/01 14:14:37 UTC
svn commit: r1717424 - in /sling/trunk/bundles/commons/threads/src:
main/java/org/apache/sling/commons/threads/impl/
test/java/org/apache/sling/commons/threads/impl/
Author: jsedding
Date: Tue Dec 1 13:14:37 2015
New Revision: 1717424
URL: http://svn.apache.org/viewvc?rev=1717424&view=rev
Log:
SLING-5343 - Meaningful thread names
Added:
sling/trunk/bundles/commons/threads/src/test/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactoryTest.java (with props)
Modified:
sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java
sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java
Modified: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java?rev=1717424&r1=1717423&r2=1717424&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java (original)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java Tue Dec 1 13:14:37 2015
@@ -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) {
Modified: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java?rev=1717424&r1=1717423&r2=1717424&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java (original)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java Tue Dec 1 13:14:37 2015
@@ -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.
*/
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
*/
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;
+ }
}
Added: sling/trunk/bundles/commons/threads/src/test/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactoryTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/test/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactoryTest.java?rev=1717424&view=auto
==============================================================================
--- sling/trunk/bundles/commons/threads/src/test/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactoryTest.java (added)
+++ sling/trunk/bundles/commons/threads/src/test/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactoryTest.java Tue Dec 1 13:14:37 2015
@@ -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
+ );
+ }
+}
Propchange: sling/trunk/bundles/commons/threads/src/test/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactoryTest.java
------------------------------------------------------------------------------
svn:eol-style = native