src/java.management/share/classes/sun/management/Sensor.java
author naoto
Thu, 07 Nov 2019 10:06:04 -0800
changeset 58969 a4430bb9f97d
parent 49558 b2bd13eafc1c
permissions -rw-r--r--
8233579: DateFormatSymbols.getShortMonths() return wrong string on es_CL, es_CO locales Reviewed-by: joehw
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>
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 30655
diff changeset
    37
 * An {@code AbstractSensor} object consists of two attributes:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <ul>
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    39
 *   <li>{@code on} is a boolean flag indicating if a sensor is
2
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>
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    42
 *   <li>{@code count} is the total number of times that a sensor
2
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 {
49558
b2bd13eafc1c 8201327: Make Sensor deeply immutably thread safe
martin
parents: 47216
diff changeset
    51
    private final Object lock = new Object();
b2bd13eafc1c 8201327: Make Sensor deeply immutably thread safe
martin
parents: 47216
diff changeset
    52
    private final String name;
b2bd13eafc1c 8201327: Make Sensor deeply immutably thread safe
martin
parents: 47216
diff changeset
    53
    private long count;                 // VM-initialized to 0
b2bd13eafc1c 8201327: Make Sensor deeply immutably thread safe
martin
parents: 47216
diff changeset
    54
    private boolean on;                 // VM-initialized to false
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    57
     * Constructs a {@code Sensor} object.
2
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Returns the name of this sensor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @return the name of this sensor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns the total number of times that this sensor has been triggered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @return the total number of times that this sensor has been triggered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public long getCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Tests if this sensor is currently on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    88
     * @return {@code true} if the sensor is currently on;
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    89
     *         {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public boolean isOn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Triggers this sensor. This method first sets this sensor on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * and increments its sensor count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public void trigger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            on = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        triggerAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Triggers this sensor. This method sets this sensor on
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   112
     * and increments the count with the input {@code increment}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void trigger(int increment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            on = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            count += increment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // Do something here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        triggerAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Triggers this sensor piggybacking a memory usage object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * This method sets this sensor on
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   126
     * and increments the count with the input {@code increment}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public void trigger(int increment, MemoryUsage usage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            on = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            count += increment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            // Do something here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        triggerAction(usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Clears this sensor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            on = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        clearAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Clears this sensor
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   150
     * and increments the count with the input {@code increment}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void clear(int increment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            on = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            count += increment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        clearAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return "Sensor - " + getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            (isOn() ? " on " : " off ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            " count = " + getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    abstract void triggerAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    abstract void triggerAction(MemoryUsage u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    abstract void clearAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}