diff --git a/src/org/jitsi/impl/neomedia/transform/csrc/CsrcAudioLevelDispatcher.java b/src/org/jitsi/impl/neomedia/transform/csrc/CsrcAudioLevelDispatcher.java index eb0db1c9106bb53040b851caa4374c22dab25d4a..4cb22ed0afb19771f27722b2079fef3c420fa56c 100644 --- a/src/org/jitsi/impl/neomedia/transform/csrc/CsrcAudioLevelDispatcher.java +++ b/src/org/jitsi/impl/neomedia/transform/csrc/CsrcAudioLevelDispatcher.java @@ -1,8 +1,15 @@ +/* + * Jitsi, the OpenSource Java VoIP and Instant Messaging client. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ package org.jitsi.impl.neomedia.transform.csrc; import java.util.concurrent.*; import org.jitsi.impl.neomedia.*; +import org.jitsi.util.*; /** * A simple thread that waits for new levels to be reported from incoming @@ -21,41 +28,9 @@ public class CsrcAudioLevelDispatcher * <tt>CsrcAudioLevelDispatcher</tt>s. */ private static final ExecutorService threadPool - = Executors.newCachedThreadPool( - new ThreadFactory() - { - /** - * The default <tt>ThreadFactory</tt> implementation which - * is augmented by this instance to create daemon - * <tt>Thread</tt>s. - */ - private final ThreadFactory defaultThreadFactory - = Executors.defaultThreadFactory(); - - @Override - public Thread newThread(Runnable r) - { - Thread t = defaultThreadFactory.newThread(r); - - if (t != null) - { - t.setDaemon(true); - - /* - * Additionally, make it known through the name of - * the Thread that it is associated with the - * CsrcAudioLevelDispatcher class for - * debugging/informational purposes. - */ - String name = t.getName(); - - if (name == null) - name = ""; - t.setName("CsrcAudioLevelDispatcher-" + name); - } - return t; - } - }); + = ExecutorUtils.newCachedThreadPool( + true, + "CsrcAudioLevelDispatcher"); /** The levels that we last received from the reverseTransform thread*/ private long[] lastReportedLevels = null; diff --git a/src/org/jitsi/util/ExecutorUtils.java b/src/org/jitsi/util/ExecutorUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..e91cebf96004839fca6b8562e789f64097562106 --- /dev/null +++ b/src/org/jitsi/util/ExecutorUtils.java @@ -0,0 +1,76 @@ +/* + * Jitsi, the OpenSource Java VoIP and Instant Messaging client. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.jitsi.util; + +import java.util.concurrent.*; + +/** + * Implements utility functions to facilitate work with <tt>Executor</tt>s and + * <tt>ExecutorService</tt>. + * + * @author Lyubomir Marinov + */ +public class ExecutorUtils +{ + /** + * Creates a thread pool that creates new threads as needed, but will reuse + * previously constructed threads when they are available. Optionally, the + * new threads are created as daemon threads and their names are based on a + * specific (prefix) string. + * + * @param daemon <tt>true</tt> to create the new threads as daemon threads + * or <tt>false</tt> to create the new threads as user threads + * @param baseName the base/prefix to use for the names of the new threads + * or <tt>null</tt> to leave them with their default names + * @return the newly created thread pool + */ + public static ExecutorService newCachedThreadPool( + final boolean daemon, + final String baseName) + { + return + Executors.newCachedThreadPool( + new ThreadFactory() + { + /** + * The default <tt>ThreadFactory</tt> implementation + * which is augmented by this instance to create daemon + * <tt>Thread</tt>s. + */ + private final ThreadFactory defaultThreadFactory + = Executors.defaultThreadFactory(); + + @Override + public Thread newThread(Runnable r) + { + Thread t = defaultThreadFactory.newThread(r); + + if (t != null) + { + t.setDaemon(daemon); + + /* + * Additionally, make it known through the name + * of the Thread that it is associated with the + * specified class for debugging/informational + * purposes. + */ + if ((baseName != null) + && (baseName.length() != 0)) + { + String name = t.getName(); + + if (name == null) + name = ""; + t.setName(baseName + "-" + name); + } + } + return t; + } + }); + } +}