Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/management/Sensor.java
38827 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* A <tt>AbstractSensor</tt> object consists of two attributes:37* <ul>38* <li><tt>on</tt> 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><tt>count</tt> 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 Object lock;51private String name;52private long count;53private boolean on;5455/**56* Constructs a <tt>Sensor</tt> object.57*58* @param name The name of this sensor.59*/60public Sensor(String name) {61this.name = name;62this.count = 0;63this.on = false;64this.lock = new Object();65}6667/**68* Returns the name of this sensor.69*70* @return the name of this sensor.71*/72public String getName() {73return name;74}7576/**77* Returns the total number of times that this sensor has been triggered.78*79* @return the total number of times that this sensor has been triggered.80*/81public long getCount() {82synchronized (lock) {83return count;84}85}8687/**88* Tests if this sensor is currently on.89*90* @return <tt>true</tt> if the sensor is currently on;91* <tt>false</tt> otherwise.92*93*/94public boolean isOn() {95synchronized (lock) {96return on;97}98}99100/**101* Triggers this sensor. This method first sets this sensor on102* and increments its sensor count.103*/104public void trigger() {105synchronized (lock) {106on = true;107count++;108}109triggerAction();110}111112/**113* Triggers this sensor. This method sets this sensor on114* and increments the count with the input <tt>increment</tt>.115*/116public void trigger(int increment) {117synchronized (lock) {118on = true;119count += increment;120// Do something here...121}122triggerAction();123}124125/**126* Triggers this sensor piggybacking a memory usage object.127* This method sets this sensor on128* and increments the count with the input <tt>increment</tt>.129*/130public void trigger(int increment, MemoryUsage usage) {131synchronized (lock) {132on = true;133count += increment;134// Do something here...135}136triggerAction(usage);137}138139/**140* Clears this sensor.141*/142public void clear() {143synchronized (lock) {144on = false;145}146clearAction();147}148149150/**151* Clears this sensor152* and increments the count with the input <tt>increment</tt>.153*/154public void clear(int increment) {155synchronized (lock) {156on = false;157count += increment;158}159clearAction();160}161162public String toString() {163return "Sensor - " + getName() +164(isOn() ? " on " : " off ") +165" count = " + getCount();166}167168abstract void triggerAction();169abstract void triggerAction(MemoryUsage u);170abstract void clearAction();171}172173174