Path: blob/master/src/java.management/share/classes/java/lang/management/LockInfo.java
41159 views
/*1* Copyright (c) 2005, 2019, 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 java.lang.management;2627import javax.management.openmbean.CompositeData;28import java.util.concurrent.locks.*;29import sun.management.LockInfoCompositeData;3031/**32* Information about a <em>lock</em>. A lock can be a built-in object monitor,33* an <em>ownable synchronizer</em>, or the {@link Condition Condition}34* object associated with synchronizers.35* <p>36* <a id="OwnableSynchronizer">An ownable synchronizer</a> is37* a synchronizer that may be exclusively owned by a thread and uses38* {@link AbstractOwnableSynchronizer AbstractOwnableSynchronizer}39* (or its subclass) to implement its synchronization property.40* {@link ReentrantLock ReentrantLock} and the write-lock (but not41* the read-lock) of {@link ReentrantReadWriteLock ReentrantReadWriteLock} are42* two examples of ownable synchronizers provided by the platform.43*44* <h2><a id="MappedType">MXBean Mapping</a></h2>45* {@code LockInfo} is mapped to a {@link CompositeData CompositeData}46* as specified in the {@link #from from} method.47*48* @see java.util.concurrent.locks.AbstractOwnableSynchronizer49* @see java.util.concurrent.locks.Condition50*51* @author Mandy Chung52* @since 1.653*/5455public class LockInfo {5657private String className;58private int identityHashCode;5960/**61* Constructs a {@code LockInfo} object.62*63* @param className the fully qualified name of the class of the lock object.64* @param identityHashCode the {@link System#identityHashCode65* identity hash code} of the lock object.66*/67public LockInfo(String className, int identityHashCode) {68if (className == null) {69throw new NullPointerException("Parameter className cannot be null");70}71this.className = className;72this.identityHashCode = identityHashCode;73}7475/**76* package-private constructors77*/78LockInfo(Object lock) {79this.className = lock.getClass().getName();80this.identityHashCode = System.identityHashCode(lock);81}8283/**84* Returns the fully qualified name of the class of the lock object.85*86* @return the fully qualified name of the class of the lock object.87*/88public String getClassName() {89return className;90}9192/**93* Returns the identity hash code of the lock object94* returned from the {@link System#identityHashCode} method.95*96* @return the identity hash code of the lock object.97*/98public int getIdentityHashCode() {99return identityHashCode;100}101102/**103* Returns a {@code LockInfo} object represented by the104* given {@code CompositeData}.105* The given {@code CompositeData} must contain the following attributes:106* <table class="striped" style="margin-left:2em;">107* <caption style="display:none">The attributes and the types the given CompositeData contains</caption>108* <thead style="text-align:left">109* <tr>110* <th scope="col">Attribute Name</th>111* <th scope="col">Type</th>112* </tr>113* </thead>114* <tbody style="text-align:left">115* <tr>116* <th scope="row">className</th>117* <td>{@code java.lang.String}</td>118* </tr>119* <tr>120* <th scope="row">identityHashCode</th>121* <td>{@code java.lang.Integer}</td>122* </tr>123* </tbody>124* </table>125*126* @param cd {@code CompositeData} representing a {@code LockInfo}127*128* @throws IllegalArgumentException if {@code cd} does not129* represent a {@code LockInfo} with the attributes described130* above.131* @return a {@code LockInfo} object represented132* by {@code cd} if {@code cd} is not {@code null};133* {@code null} otherwise.134*135* @since 1.8136*/137public static LockInfo from(CompositeData cd) {138if (cd == null) {139return null;140}141142if (cd instanceof LockInfoCompositeData) {143return ((LockInfoCompositeData) cd).getLockInfo();144} else {145return LockInfoCompositeData.toLockInfo(cd);146}147}148149/**150* Returns a string representation of a lock. The returned151* string representation consists of the name of the class of the152* lock object, the at-sign character `@', and the unsigned153* hexadecimal representation of the <em>identity</em> hash code154* of the object. This method returns a string equals to the value of:155* <blockquote>156* <pre>157* lock.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(lock))158* </pre></blockquote>159* where {@code lock} is the lock object.160*161* @return the string representation of a lock.162*/163public String toString() {164return className + '@' + Integer.toHexString(identityHashCode);165}166}167168169