src/java.management/share/classes/sun/management/ThreadImpl.java
changeset 58223 778fc2dcbdaa
parent 58208 0f3c23c374a4
child 58348 c29e49148be7
--- a/src/java.management/share/classes/sun/management/ThreadImpl.java	Wed Sep 18 17:31:35 2019 -0400
+++ b/src/java.management/share/classes/sun/management/ThreadImpl.java	Wed Sep 18 20:49:13 2019 -0400
@@ -29,7 +29,6 @@
 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
@@ -113,15 +112,11 @@
         return cpuTimeEnabled;
     }
 
-    private void ensureThreadAllocatedMemorySupported() {
+    protected boolean isThreadAllocatedMemoryEnabled() {
         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;
     }
 
@@ -160,18 +155,16 @@
         return getThreadInfo(ids, 0);
     }
 
-    private void verifyThreadId(long id) {
-        if (id <= 0) {
-            throw new IllegalArgumentException(
-                "Invalid thread ID parameter: " + id);
+    private void verifyThreadIds(long[] ids) {
+        if (ids == null) {
+            throw new NullPointerException("Null ids parameter.");
         }
-    }
-
-    private void verifyThreadIds(long[] ids) {
-        Objects.requireNonNull(ids);
 
         for (int i = 0; i < ids.length; i++) {
-            verifyThreadId(ids[i]);
+            if (ids[i] <= 0) {
+                throw new IllegalArgumentException(
+                    "Invalid thread ID parameter: " + ids[i]);
+            }
         }
     }
 
@@ -349,41 +342,26 @@
         }
     }
 
-    protected long getCurrentThreadAllocatedBytes() {
-        if (isThreadAllocatedMemoryEnabled()) {
-            return getThreadAllocatedMemory0(0);
-        }
-        return -1;
-    }
-
-    private boolean verifyThreadAllocatedMemory(long id) {
-        verifyThreadId(id);
-        return isThreadAllocatedMemoryEnabled();
-    }
-
     protected long getThreadAllocatedBytes(long id) {
-        boolean verified = verifyThreadAllocatedMemory(id);
-
-        if (verified) {
-            return getThreadAllocatedMemory0(
-                Thread.currentThread().getId() == id ? 0 : id);
-        }
-        return -1;
+        long[] ids = new long[1];
+        ids[0] = id;
+        final long[] sizes = getThreadAllocatedBytes(ids);
+        return sizes[0];
     }
 
     private boolean verifyThreadAllocatedMemory(long[] ids) {
         verifyThreadIds(ids);
+
+        // check if Thread allocated memory measurement is supported.
+        if (!isThreadAllocatedMemorySupported()) {
+            throw new UnsupportedOperationException(
+                "Thread allocated memory measurement is not supported.");
+        }
+
         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];
@@ -396,7 +374,10 @@
     }
 
     protected void setThreadAllocatedMemoryEnabled(boolean enable) {
-        ensureThreadAllocatedMemorySupported();
+        if (!isThreadAllocatedMemorySupported()) {
+            throw new UnsupportedOperationException(
+                "Thread allocated memory measurement is not supported.");
+        }
 
         Util.checkControlAccess();
         synchronized (this) {
@@ -530,7 +511,6 @@
     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);