[jboss-remoting-commits] JBoss Remoting SVN: r4018 - remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Fri Apr 18 19:01:53 EDT 2008


Author: david.lloyd at jboss.com
Date: 2008-04-18 19:01:53 -0400 (Fri, 18 Apr 2008)
New Revision: 4018

Added:
   remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util/NamingThreadFactory.java
Log:
Naming thread factory class

Added: remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util/NamingThreadFactory.java
===================================================================
--- remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util/NamingThreadFactory.java	                        (rev 0)
+++ remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util/NamingThreadFactory.java	2008-04-18 23:01:53 UTC (rev 4018)
@@ -0,0 +1,35 @@
+package org.jboss.cx.remoting.util;
+
+import java.util.concurrent.ThreadFactory;
+
+/**
+ * A wrapper {@code ThreadFactory} that gives the threads a sensible name.
+ */
+public final class NamingThreadFactory implements ThreadFactory {
+    private final ThreadFactory delegate;
+    private final String namePattern;
+
+    /**
+     * Create a new instance.  The pattern is a format string such as one would provide to
+     * {@link String#format(String, Object[])}.  The format string should contain one {@code %s} which will be replaced
+     * with the original thread name.
+     *
+     * @param delegate the thread factory to actually use to produce the thread
+     * @param namePattern the name pattern to apply
+     */
+    public NamingThreadFactory(final ThreadFactory delegate, final String namePattern) {
+        this.delegate = delegate;
+        this.namePattern = namePattern;
+    }
+
+    public Thread newThread(final Runnable r) {
+        final Thread thread = delegate.newThread(r);
+        final String originalName = thread.getName();
+        try {
+            thread.setName(String.format(namePattern, originalName));
+        } catch (SecurityException ex) {
+            // oh well, we tried
+        }
+        return thread;
+    }
+}




More information about the jboss-remoting-commits mailing list