src/share/classes/sun/misc/VM.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 

  28 import java.util.Properties;
  29 import java.util.HashMap;
  30 import java.util.Map;
  31 import java.util.Set;
  32 
  33 public class VM {
  34 
  35     /* The following methods used to be native methods that instruct
  36      * the VM to selectively suspend certain threads in low-memory
  37      * situations. They are inherently dangerous and not implementable
  38      * on native threads. We removed them in JDK 1.2. The skeletons
  39      * remain so that existing applications that use these methods
  40      * will still work.
  41      */
  42     private static boolean suspended = false;
  43 
  44     /** @deprecated */
  45     @Deprecated
  46     public static boolean threadsSuspended() {
  47         return suspended;


 315      */
 316     public static int getPeakFinalRefCount() {
 317         return peakFinalRefCount;
 318     }
 319 
 320     /*
 321      * Add <tt>n</tt> to the objects pending for finalization count.
 322      *
 323      * @param n an integer value to be added to the objects pending
 324      * for finalization count
 325      */
 326     public static void addFinalRefCount(int n) {
 327         // The caller must hold lock to synchronize the update.
 328 
 329         finalRefCount += n;
 330         if (finalRefCount > peakFinalRefCount) {
 331             peakFinalRefCount = finalRefCount;
 332         }
 333     }
 334 
 335 


 336     public static Thread.State toThreadState(int threadStatus) {
 337         // Initialize the threadStateMap
 338         initThreadStateMap();
 339 
 340         Thread.State s = threadStateMap.get(threadStatus);
 341         if (s == null) {
 342             // default to RUNNABLE if the threadStatus value is unknown
 343             s = Thread.State.RUNNABLE;







 344         }
 345         return s;
 346     }
 347 
 348     // a map of threadStatus values to the corresponding Thread.State
 349     private static Map<Integer, Thread.State> threadStateMap = null;
 350     private static Map<Integer, String> threadStateNames = null;








 351 
 352     private synchronized static void initThreadStateMap() {
 353         if (threadStateMap != null) {
 354             return;
 355         }
 356 
 357         final Thread.State[] ts = Thread.State.values();
 358 
 359         final int[][] vmThreadStateValues = new int[ts.length][];
 360         final String[][] vmThreadStateNames = new String[ts.length][];
 361         getThreadStateValues(vmThreadStateValues, vmThreadStateNames);
 362 
 363         threadStateMap = new HashMap<Integer, Thread.State>();
 364         threadStateNames = new HashMap<Integer, String>();
 365         for (int i = 0; i < ts.length; i++) {
 366             String state = ts[i].name();
 367             int[] values = null;
 368             String[] names = null;
 369             for (int j = 0; j < ts.length; j++) {
 370                 if (vmThreadStateNames[j][0].startsWith(state)) {
 371                     values = vmThreadStateValues[j];
 372                     names = vmThreadStateNames[j];
 373                 }
 374             }
 375             if (values == null) {
 376                 throw new InternalError("No VM thread state mapped to " +
 377                     state);
 378             }
 379             if (values.length != names.length) {
 380                 throw new InternalError("VM thread state values and names " +
 381                     " mapped to " + state + ": length not matched" );
 382             }
 383             for (int k = 0; k < values.length; k++) {
 384                 threadStateMap.put(values[k], ts[i]);
 385                 threadStateNames.put(values[k], names[k]);
 386             }
 387         }
 388     }
 389     // Fill in vmThreadStateValues with int arrays, each of which contains
 390     // the threadStatus values mapping to the Thread.State enum constant.
 391     // Fill in vmThreadStateNames with String arrays, each of which contains
 392     // the name of each threadStatus value of the format:
 393     //    <Thread.State.name()>[.<Substate name>]
 394     // e.g. WAITING.OBJECT_WAIT
 395     //
 396     private native static void getThreadStateValues(int[][] vmThreadStateValues,
 397                                                     String[][] vmThreadStateNames);
 398 
 399     static {
 400         initialize();
 401     }
 402     private native static void initialize();
 403 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 import static java.lang.Thread.State.*;
  29 import java.util.Properties;
  30 import java.util.HashMap;
  31 import java.util.Map;
  32 import java.util.Set;
  33 
  34 public class VM {
  35 
  36     /* The following methods used to be native methods that instruct
  37      * the VM to selectively suspend certain threads in low-memory
  38      * situations. They are inherently dangerous and not implementable
  39      * on native threads. We removed them in JDK 1.2. The skeletons
  40      * remain so that existing applications that use these methods
  41      * will still work.
  42      */
  43     private static boolean suspended = false;
  44 
  45     /** @deprecated */
  46     @Deprecated
  47     public static boolean threadsSuspended() {
  48         return suspended;


 316      */
 317     public static int getPeakFinalRefCount() {
 318         return peakFinalRefCount;
 319     }
 320 
 321     /*
 322      * Add <tt>n</tt> to the objects pending for finalization count.
 323      *
 324      * @param n an integer value to be added to the objects pending
 325      * for finalization count
 326      */
 327     public static void addFinalRefCount(int n) {
 328         // The caller must hold lock to synchronize the update.
 329 
 330         finalRefCount += n;
 331         if (finalRefCount > peakFinalRefCount) {
 332             peakFinalRefCount = finalRefCount;
 333         }
 334     }
 335 
 336     /**
 337      * Returns Thread.State for the given threadStatus
 338      */
 339     public static Thread.State toThreadState(int threadStatus) {
 340         if ((threadStatus & JVMTI_THREAD_STATE_RUNNABLE) != 0) {
 341             return RUNNABLE;
 342         } else if ((threadStatus & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) != 0) {
 343             return BLOCKED;
 344         } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_INDEFINITELY) != 0) {
 345             return WAITING;
 346         } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT) != 0) {
 347             return TIMED_WAITING;
 348         } else if ((threadStatus & JVMTI_THREAD_STATE_TERMINATED) != 0) {
 349             return TERMINATED;
 350         } else if ((threadStatus & JVMTI_THREAD_STATE_ALIVE) == 0) {
 351             return NEW;
 352         } else {
 353             return RUNNABLE;
 354         }

 355     }
 356 
 357     /* The threadStatus field is set by the VM at state transition
 358      * in the hotspot implementation. Its value is set according to
 359      * the JVM TI specification GetThreadState function
 360      */
 361     private final static int JVMTI_THREAD_STATE_ALIVE = 0x0001;
 362     private final static int JVMTI_THREAD_STATE_TERMINATED = 0x0002;
 363     private final static int JVMTI_THREAD_STATE_RUNNABLE = 0x0004;
 364     private final static int JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER = 0x0400;
 365     private final static int JVMTI_THREAD_STATE_WAITING = 0x0080;
 366     private final static int JVMTI_THREAD_STATE_WAITING_INDEFINITELY = 0x0010;
 367     private final static int JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT = 0x0020;
 368 















































 369     static {
 370         initialize();
 371     }
 372     private native static void initialize();
 373 }