View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  package org.perfidix.meter;
28  
29  /**
30   * This class measures the number of threads in the runtime.
31   * 
32   * @author Sebastian Graf, University of Konstanz
33   */
34  public final class ThreadMeter extends AbstractMeter {
35  
36      /**
37       * the threadgroup to analyse.
38       */
39      private transient final ThreadGroup topThreadGroup;
40  
41      /** Name of the Meter. */
42      private static final String NAME = "ThreadMeter";
43  
44      /** Unit of the Meter. */
45      private static final String UNIT = "threads";
46  
47      /** UnitDescription of the Meter. */
48      private static final String DESCRIPTION = "Number of Threads";
49  
50      /**
51       * Constructor;
52       */
53      public ThreadMeter() {
54          super();
55          final Thread ownThread = Thread.currentThread();
56          final ThreadGroup ownGroup = ownThread.getThreadGroup();
57          ThreadGroup group = null;
58          do {
59              group = ownGroup.getParent();
60          } while (group.getParent() != null);
61          this.topThreadGroup = group;
62      }
63  
64      /**
65       * Constructor.
66       * 
67       * @param group
68       *            the group of number of threads to analyse.
69       */
70      public ThreadMeter(final ThreadGroup group) {
71          super();
72          this.topThreadGroup = group;
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public String getName() {
78          return NAME;
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      public String getUnit() {
84          return UNIT;
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public String getUnitDescription() {
90          return DESCRIPTION;
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public double getValue() {
96          return topThreadGroup.activeCount();
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     public int hashCode() {
102         final int prime = 31;
103         int result = prime;
104         result = prime * result;
105 
106         return result;
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public boolean equals(final Object obj) {
112         boolean returnVal = true;
113         if (this == obj) {
114             returnVal = true;
115         }
116         if (getClass() == obj.getClass()) {
117             returnVal = true;
118         } else {
119             returnVal = false;
120         }
121         return returnVal;
122     }
123 
124 }