View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.constructs.nonstop.util;
18  
19  import java.util.concurrent.ThreadFactory;
20  import java.util.concurrent.atomic.AtomicInteger;
21  
22  /***
23   * A custom {@link ThreadFactory} that maintains a count of how many threads this factory has created
24   *
25   * @author Abhishek Sanoujam
26   *
27   */
28  public class CountingThreadFactory implements ThreadFactory {
29  
30      private final AtomicInteger count = new AtomicInteger();
31      private final ThreadFactory actualFactory;
32  
33      /***
34       * Constructor accepting the actual thread factory that will create the threads
35       *
36       * @param actualFactory
37       *            the actual factory
38       */
39      public CountingThreadFactory(ThreadFactory actualFactory) {
40          this.actualFactory = actualFactory;
41      }
42  
43      /***
44       * {@inheritDoc}
45       */
46      public Thread newThread(Runnable r) {
47          Thread newThread = actualFactory.newThread(new RunnableWithLifeCycle(this, r));
48          if (newThread != null) {
49              count.incrementAndGet();
50          }
51          return newThread;
52      }
53  
54      /***
55       * Returns the number of threads this factory has created and currently alive
56       *
57       * @return Returns the number of threads this factory has created and currently alive
58       */
59      public int getNumberOfThreads() {
60          return count.get();
61      }
62  
63      private void threadExecutionComplete() {
64          count.decrementAndGet();
65      }
66  
67      /***
68       * Runnable which also fires lifecycle events
69       *
70       * @author Abhishek Sanoujam
71       *
72       */
73      private static class RunnableWithLifeCycle implements Runnable {
74  
75          private final Runnable actualRunnable;
76          private final CountingThreadFactory countingThreadFactory;
77  
78          /***
79           * Constructor accepting a {@link CountingThreadFactory} and the actual runnable
80           *
81           * @param countingThreadFactory
82           * @param actualRunnable
83           */
84          public RunnableWithLifeCycle(CountingThreadFactory countingThreadFactory, Runnable actualRunnable) {
85              super();
86              this.countingThreadFactory = countingThreadFactory;
87              this.actualRunnable = actualRunnable;
88          }
89  
90          /***
91           * {@inheritDoc}
92           */
93          public void run() {
94              try {
95                  if (actualRunnable != null) {
96                      actualRunnable.run();
97                  }
98              } finally {
99                  countingThreadFactory.threadExecutionComplete();
100             }
101         }
102 
103     }
104 
105 }