Path: blob/master/src/java.management/share/classes/sun/management/Sensor.java
41152 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management;2627import java.lang.management.MemoryUsage;28import java.util.Iterator;29import java.util.Map;30import java.util.HashMap;3132/**33* An abstract sensor.34*35* <p>36* An {@code AbstractSensor} object consists of two attributes:37* <ul>38* <li>{@code on} is a boolean flag indicating if a sensor is39* triggered. This flag will be set or cleared by the40* component that owns the sensor.</li>41* <li>{@code count} is the total number of times that a sensor42* has been triggered.</li>43* </ul>44*45* @author Mandy Chung46* @since 1.547*/4849public abstract class Sensor {50private final Object lock = new Object();51private final String name;52private long count; // VM-initialized to 053private boolean on; // VM-initialized to false5455/**56* Constructs a {@code Sensor} object.57*58* @param name The name of this sensor.59*/60public Sensor(String name) {61this.name = name;62}6364/**65* Returns the name of this sensor.66*67* @return the name of this sensor.68*/69public String getName() {70return name;71}7273/**74* Returns the total number of times that this sensor has been triggered.75*76* @return the total number of times that this sensor has been triggered.77*/78public long getCount() {79synchronized (lock) {80return count;81}82}8384/**85* Tests if this sensor is currently on.86*87* @return {@code true} if the sensor is currently on;88* {@code false} otherwise.89*90*/91public boolean isOn() {92synchronized (lock) {93return on;94}95}9697/**98* Triggers this sensor. This method first sets this sensor on99* and increments its sensor count.100*/101public void trigger() {102synchronized (lock) {103on = true;104count++;105}106triggerAction();107}108109/**110* Triggers this sensor. This method sets this sensor on111* and increments the count with the input {@code increment}.112*/113public void trigger(int increment) {114synchronized (lock) {115on = true;116count += increment;117// Do something here...118}119triggerAction();120}121122/**123* Triggers this sensor piggybacking a memory usage object.124* This method sets this sensor on125* and increments the count with the input {@code increment}.126*/127public void trigger(int increment, MemoryUsage usage) {128synchronized (lock) {129on = true;130count += increment;131// Do something here...132}133triggerAction(usage);134}135136/**137* Clears this sensor.138*/139public void clear() {140synchronized (lock) {141on = false;142}143clearAction();144}145146147/**148* Clears this sensor149* and increments the count with the input {@code increment}.150*/151public void clear(int increment) {152synchronized (lock) {153on = false;154count += increment;155}156clearAction();157}158159public String toString() {160return "Sensor - " + getName() +161(isOn() ? " on " : " off ") +162" count = " + getCount();163}164165abstract void triggerAction();166abstract void triggerAction(MemoryUsage u);167abstract void clearAction();168}169170171