< prev index next >

src/java.management/share/classes/sun/management/ThreadImpl.java

Print this page

        

@@ -27,10 +27,11 @@
 
 import java.lang.management.ManagementFactory;
 import java.lang.management.ThreadInfo;
 import java.lang.management.ThreadMXBean;
 import javax.management.ObjectName;
+import java.util.Objects;
 
 /**
  * Implementation for java.lang.management.ThreadMXBean as well as providing the
  * supporting method for com.sun.management.ThreadMXBean.
  * The supporting method for com.sun.management.ThreadMXBean can be moved to

@@ -110,15 +111,19 @@
                 "Thread CPU time measurement is not supported");
         }
         return cpuTimeEnabled;
     }
 
-    protected boolean isThreadAllocatedMemoryEnabled() {
+    private void ensureThreadAllocatedMemorySupported() {
         if (!isThreadAllocatedMemorySupported()) {
             throw new UnsupportedOperationException(
-                "Thread allocated memory measurement is not supported");
+                "Thread allocated memory measurement is not supported.");
+        }
         }
+
+    protected boolean isThreadAllocatedMemoryEnabled() {
+        ensureThreadAllocatedMemorySupported();
         return allocatedMemoryEnabled;
     }
 
     @Override
     public long[] getAllThreadIds() {

@@ -153,20 +158,22 @@
     @Override
     public ThreadInfo[] getThreadInfo(long[] ids) {
         return getThreadInfo(ids, 0);
     }
 
-    private void verifyThreadIds(long[] ids) {
-        if (ids == null) {
-            throw new NullPointerException("Null ids parameter.");
+    private void verifyThreadId(long id) {
+        if (id <= 0) {
+            throw new IllegalArgumentException(
+                "Invalid thread ID parameter: " + id);
+        }
         }
 
+    private void verifyThreadIds(long[] ids) {
+        Objects.requireNonNull(ids);
+
         for (int i = 0; i < ids.length; i++) {
-            if (ids[i] <= 0) {
-                throw new IllegalArgumentException(
-                    "Invalid thread ID parameter: " + ids[i]);
-            }
+            verifyThreadId(ids[i]);
         }
     }
 
     @Override
     public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {

@@ -340,30 +347,45 @@
                 cpuTimeEnabled = enable;
             }
         }
     }
 
-    protected long getThreadAllocatedBytes(long id) {
-        long[] ids = new long[1];
-        ids[0] = id;
-        final long[] sizes = getThreadAllocatedBytes(ids);
-        return sizes[0];
+    protected long getCurrentThreadAllocatedBytes() {
+        if (isThreadAllocatedMemoryEnabled()) {
+            return getThreadAllocatedMemory0(0);
+        }
+        return -1;
     }
 
-    private boolean verifyThreadAllocatedMemory(long[] ids) {
-        verifyThreadIds(ids);
+    private boolean verifyThreadAllocatedMemory(long id) {
+        verifyThreadId(id);
+        return isThreadAllocatedMemoryEnabled();
+    }
 
-        // check if Thread allocated memory measurement is supported.
-        if (!isThreadAllocatedMemorySupported()) {
-            throw new UnsupportedOperationException(
-                "Thread allocated memory measurement is not supported.");
+    protected long getThreadAllocatedBytes(long id) {
+        boolean verified = verifyThreadAllocatedMemory(id);
+
+        if (verified) {
+            return getThreadAllocatedMemory0(
+                Thread.currentThread().getId() == id ? 0 : id);
+        }
+        return -1;
         }
 
+    private boolean verifyThreadAllocatedMemory(long[] ids) {
+        verifyThreadIds(ids);
         return isThreadAllocatedMemoryEnabled();
     }
 
     protected long[] getThreadAllocatedBytes(long[] ids) {
+        Objects.requireNonNull(ids);
+
+        if (ids.length == 1) {
+            long size = getThreadAllocatedBytes(ids[0]);
+            return new long[] { size };
+        }
+
         boolean verified = verifyThreadAllocatedMemory(ids);
 
         long[] sizes = new long[ids.length];
         java.util.Arrays.fill(sizes, -1);
 

@@ -372,14 +394,11 @@
         }
         return sizes;
     }
 
     protected void setThreadAllocatedMemoryEnabled(boolean enable) {
-        if (!isThreadAllocatedMemorySupported()) {
-            throw new UnsupportedOperationException(
-                "Thread allocated memory measurement is not supported.");
-        }
+        ensureThreadAllocatedMemorySupported();
 
         Util.checkControlAccess();
         synchronized (this) {
             if (allocatedMemoryEnabled != enable) {
                 // notify VM of the state change

@@ -509,10 +528,11 @@
                                               ThreadInfo[] result);
     private static native long getThreadTotalCpuTime0(long id);
     private static native void getThreadTotalCpuTime1(long[] ids, long[] result);
     private static native long getThreadUserCpuTime0(long id);
     private static native void getThreadUserCpuTime1(long[] ids, long[] result);
+    private static native long getThreadAllocatedMemory0(long id);
     private static native void getThreadAllocatedMemory1(long[] ids, long[] result);
     private static native void setThreadCpuTimeEnabled0(boolean enable);
     private static native void setThreadAllocatedMemoryEnabled0(boolean enable);
     private static native void setThreadContentionMonitoringEnabled0(boolean enable);
     private static native Thread[] findMonitorDeadlockedThreads0();
< prev index next >