jdk/src/java.management/share/classes/sun/management/Sensor.java
author weijun
Thu, 11 Dec 2014 15:23:02 +0800
changeset 27957 24b4e6082f19
parent 25859 3317bb8137f4
child 30655 d83f50188ca9
permissions -rw-r--r--
8055723: Replace concat String to append in StringBuilder parameters (dev) Reviewed-by: redestad, ulfzibis, weijun, prappo, igerasim, alanb Contributed-by: Otavio Santana <otaviojava@java.net>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.management.MemoryUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * An abstract sensor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A <tt>AbstractSensor</tt> object consists of two attributes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *   <li><tt>on</tt> is a boolean flag indicating if a sensor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *       triggered. This flag will be set or cleared by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *       component that owns the sensor.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *   <li><tt>count</tt> is the total number of times that a sensor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *       has been triggered.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public abstract class Sensor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private Object  lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private String  name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private long    count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Constructs a <tt>Sensor</tt> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param name The name of this sensor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public Sensor(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.on = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Returns the name of this sensor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @return the name of this sensor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Returns the total number of times that this sensor has been triggered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @return the total number of times that this sensor has been triggered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public long getCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Tests if this sensor is currently on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @return <tt>true</tt> if the sensor is currently on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *         <tt>false</tt> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public boolean isOn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Triggers this sensor. This method first sets this sensor on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * and increments its sensor count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public void trigger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            on = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        triggerAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Triggers this sensor. This method sets this sensor on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * and increments the count with the input <tt>increment</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public void trigger(int increment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            on = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            count += increment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // Do something here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        triggerAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Triggers this sensor piggybacking a memory usage object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * This method sets this sensor on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * and increments the count with the input <tt>increment</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public void trigger(int increment, MemoryUsage usage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            on = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            count += increment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // Do something here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        triggerAction(usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Clears this sensor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            on = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        clearAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Clears this sensor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * and increments the count with the input <tt>increment</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public void clear(int increment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            on = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            count += increment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        clearAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return "Sensor - " + getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            (isOn() ? " on " : " off ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            " count = " + getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    abstract void triggerAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    abstract void triggerAction(MemoryUsage u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    abstract void clearAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}