View Javadoc

1   package de.desy.acop.displayers.tools;
2   
3   import java.util.concurrent.ThreadFactory;
4   import java.util.concurrent.atomic.AtomicInteger;
5   
6   /**
7    * Thread factory to be used with applications. 
8    * 
9    * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
10   * @version $Id: Templates.xml,v 1.10 2004/01/13 16:17:13 jbobnar Exp $
11   *
12   */
13  public class TINEThreadFactory implements ThreadFactory {
14      static final AtomicInteger poolNumber = new AtomicInteger(1);
15      final ThreadGroup group;
16      final AtomicInteger threadNumber = new AtomicInteger(1);
17      final String namePrefix;
18      private boolean lowPriority;
19  
20      /**
21       * Constructs a new factory with the given name.
22       * @param name
23       * @param lowPriortiy should the threads from this factory have priority set to low
24       */
25      public TINEThreadFactory(String name, boolean lowPriority) {
26          SecurityManager s = System.getSecurityManager();
27          group = (s != null)? s.getThreadGroup() :
28                               Thread.currentThread().getThreadGroup();
29          namePrefix = name + "-" +
30                        poolNumber.getAndIncrement() +
31                       "-thread-";
32          this.lowPriority = lowPriority;
33      }
34      
35      /**
36       * Constructs a new factory with the given name.
37       * @param name
38       */
39      public TINEThreadFactory(String name) {
40      	this(name, false);
41      }
42  
43      /*
44       * (non-Javadoc)
45       * @see java.util.concurrent.ThreadFactory#newThread(java.lang.Runnable)
46       */
47      public Thread newThread(Runnable r) {
48          Thread t = new Thread(group, r,
49                                namePrefix + threadNumber.getAndIncrement(),
50                                0);
51          if (t.isDaemon())
52              t.setDaemon(false);
53          if (t.getPriority() != Thread.NORM_PRIORITY)
54              t.setPriority(Thread.NORM_PRIORITY);
55          if (lowPriority) {
56          	t.setPriority(Thread.MIN_PRIORITY);
57          }
58          return t;
59      }
60  }