View Javadoc

1   /*
2    * $Id: ConfigChangeMonitorThreadFactory.java 19777 2010-09-29 19:44:33Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.launcher;
12  
13  import java.util.concurrent.ThreadFactory;
14  import java.util.concurrent.atomic.AtomicInteger;
15  
16  /**
17   * A slightly tweaked default thread factory that uses the following pattern:
18   * <code>[%s].config.change.%d.thread.%d</code>, where %s stands for application name,
19   * the next number will tell one how many redeployments this app had during this container's
20   * lifetime and the last digit, thread count, should always be 1. Left there for debugging
21   * purposes to quickly locate any duplicate threads trying to perform a redeploy. 
22   */
23  public class ConfigChangeMonitorThreadFactory implements ThreadFactory
24  {
25  
26      static final AtomicInteger poolNumber = new AtomicInteger(1);
27      final ThreadGroup group;
28      final AtomicInteger threadNumber = new AtomicInteger(1);
29      final String namePrefix;
30      final String appName;
31  
32      public ConfigChangeMonitorThreadFactory(String appName)
33      {
34          this.appName = appName;
35          SecurityManager s = System.getSecurityManager();
36          group = (s != null) ? s.getThreadGroup() :
37                  Thread.currentThread().getThreadGroup();
38          namePrefix = String.format("[%s].config.change.%d.thread.", appName, poolNumber.getAndIncrement());
39      }
40  
41      public Thread newThread(Runnable r)
42      {
43          Thread t = new Thread(group, r,
44                                namePrefix + threadNumber.getAndIncrement(),
45                                0);
46          if (t.isDaemon())
47          {
48              t.setDaemon(false);
49          }
50          if (t.getPriority() != Thread.NORM_PRIORITY)
51          {
52              t.setPriority(Thread.NORM_PRIORITY);
53          }
54          return t;
55      }
56  
57  
58  }